Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions src/jquery-ias.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
this.$container = (window === $element.get(0) ? $(document) : $element);
this.defaultDelay = options.delay;
this.negativeMargin = options.negativeMargin;
this.cacheAjaxResults = options.cacheAjaxResults;
this.nextUrl = null;
this.isBound = false;
this.isPaused = false;
Expand All @@ -50,7 +51,7 @@
* @private
*/
this.scrollHandler = function() {
// the throttle method can call the scrollHandler even thought we have called unbind()
// the throttle method can call the scrollHandler even though we have called unbind()
if (!this.isBound || this.isPaused) {
return;
}
Expand Down Expand Up @@ -186,31 +187,37 @@

self.fire('load', [loadEvent]);

return $.get(loadEvent.url, null, $.proxy(function(data) {
$itemContainer = $(this.itemsContainerSelector, data).eq(0);
if (0 === $itemContainer.length) {
$itemContainer = $(data).filter(this.itemsContainerSelector).eq(0);
}
return $.ajax({
url: loadEvent.url,
data: null,
success: $.proxy(function(data) {
$itemContainer = $(this.itemsContainerSelector, data).eq(0);
if (0 === $itemContainer.length) {
$itemContainer = $(data).filter(this.itemsContainerSelector).eq(0);
}

if ($itemContainer) {
$itemContainer.find(this.itemSelector).each(function() {
items.push(this);
});
}
if ($itemContainer) {
$itemContainer.find(this.itemSelector).each(function() {
items.push(this);
});
}

self.fire('loaded', [data, items]);
self.fire('loaded', [data, items]);

if (callback) {
timeDiff = +new Date() - timeStart;
if (timeDiff < delay) {
setTimeout(function() {
if (callback) {
timeDiff = +new Date() - timeStart;
if (timeDiff < delay) {
setTimeout(function() {
callback.call(self, data, items);
}, delay - timeDiff);
} else {
callback.call(self, data, items);
}, delay - timeDiff);
} else {
callback.call(self, data, items);
}
}
}
}, self), 'html');
}, self),
dataType: 'html',
cache: this.cacheAjaxResults
});
};

/**
Expand Down Expand Up @@ -245,7 +252,7 @@
}
});
});

promise.fail(function() {
if (callback) {
callback();
Expand Down Expand Up @@ -550,6 +557,9 @@
self.nextUrl = self.getNextUrl(data);

self.resume();

// The user may have scrolled further while we were paused
self.scrollHandler();
});
});
});
Expand Down Expand Up @@ -649,6 +659,7 @@
next: '.next',
pagination: false,
delay: 600,
negativeMargin: 10
negativeMargin: 10,
cacheAjaxResults: true
};
})(jQuery);