1 function maybe_load_more_content() {
2 var scroller = this; // element with scroll bars
3 var container; // element that receives new content
4 var src; // url for retrieving content
6 scrollHeight = scroller.scrollHeight || $('body')[0].scrollHeight;
7 if ($(scroller).scrollTop() + $(scroller).height()
10 container = $(this).data('infinite-container');
11 src = $(container).attr('data-infinite-content-href');
15 // Don't start another request until this one finishes
16 $(container).attr('data-infinite-content-href', null);
21 context: {container: container, src: src}}).
22 fail(function(jqxhr, status, error) {
23 if (jqxhr.readyState == 0 || jqxhr.status == 0) {
24 message = "Cancelled."
25 } else if (jqxhr.responseJSON && jqxhr.responseJSON.errors) {
26 message = jqxhr.responseJSON.errors.join("; ");
28 message = "Request failed.";
30 // TODO: report this to the user.
32 $(this.container).attr('data-infinite-content-href', this.src);
34 done(function(data, status, jqxhr) {
35 $(this.container).append(data.content);
36 $(this.container).attr('data-infinite-content-href', data.next_page_href);
37 $(document).trigger('ajax:complete');
42 on('ready ajax:complete', function() {
43 $('[data-infinite-scroller]').each(function() {
44 var $scroller = $($(this).attr('data-infinite-scroller'));
45 if (!$scroller.hasClass('smart-scroll'))
46 $scroller = $(window);
48 addClass('infinite-scroller').
49 data('infinite-container', this).
50 on('scroll', maybe_load_more_content);