From 635e9bdbc38af543ef8b46b78c3697068ce6b643 Mon Sep 17 00:00:00 2001 From: Todd Kulesza Date: Sun, 18 Jan 2015 21:20:37 -0800 Subject: [PATCH 1/4] Add option to disable caching Ajax responses. --- src/jquery-ias.js | 50 +++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/jquery-ias.js b/src/jquery-ias.js index 682305ac..98ac562c 100644 --- a/src/jquery-ias.js +++ b/src/jquery-ias.js @@ -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; @@ -185,31 +186,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 + }); }; /** @@ -627,6 +634,7 @@ next: '.next', pagination: false, delay: 600, - negativeMargin: 10 + negativeMargin: 10, + cacheAjaxResults: true }; })(jQuery); From 6f6511cb2552600ced011ab72145431c95a00359 Mon Sep 17 00:00:00 2001 From: Todd Kulesza Date: Mon, 11 May 2015 15:33:28 -0700 Subject: [PATCH 2/4] If the user scrolls very fast, ensure consecutive pages are loaded. Resolves https://github.com/webcreate/infinite-ajax-scroll/issues/167. --- src/jquery-ias.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/jquery-ias.js b/src/jquery-ias.js index 1bacd877..9b4b0aba 100644 --- a/src/jquery-ias.js +++ b/src/jquery-ias.js @@ -51,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; } @@ -252,7 +252,7 @@ } }); }); - + promise.fail(function() { if (callback) { callback(); @@ -557,6 +557,9 @@ self.nextUrl = self.getNextUrl(data); self.resume(); + + // The user may have scrolled further while we were paused + self.scrollHandler(); }); }); }); From 6122e7594f997bd977352ffb7f52a6094c41301a Mon Sep 17 00:00:00 2001 From: Todd Kulesza Date: Mon, 11 May 2015 15:42:42 -0700 Subject: [PATCH 3/4] Revert "Add option to disable caching Ajax responses." This reverts commit 635e9bdbc38af543ef8b46b78c3697068ce6b643. --- src/jquery-ias.js | 50 ++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/src/jquery-ias.js b/src/jquery-ias.js index 9b4b0aba..72934cae 100644 --- a/src/jquery-ias.js +++ b/src/jquery-ias.js @@ -26,7 +26,6 @@ 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; @@ -187,37 +186,31 @@ self.fire('load', [loadEvent]); - 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); - } + 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); + } - 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() { - callback.call(self, data, items); - }, delay - timeDiff); - } else { + if (callback) { + timeDiff = +new Date() - timeStart; + if (timeDiff < delay) { + setTimeout(function() { callback.call(self, data, items); - } + }, delay - timeDiff); + } else { + callback.call(self, data, items); } - }, self), - dataType: 'html', - cache: this.cacheAjaxResults - }); + } + }, self), 'html'); }; /** @@ -659,7 +652,6 @@ next: '.next', pagination: false, delay: 600, - negativeMargin: 10, - cacheAjaxResults: true + negativeMargin: 10 }; })(jQuery); From 20d41ccc6e0701e42a45f90073a4f174d1ab6258 Mon Sep 17 00:00:00 2001 From: Todd Kulesza Date: Mon, 11 May 2015 15:50:53 -0700 Subject: [PATCH 4/4] Revert "Revert "Add option to disable caching Ajax responses."" This reverts commit 6122e7594f997bd977352ffb7f52a6094c41301a. --- src/jquery-ias.js | 50 +++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/jquery-ias.js b/src/jquery-ias.js index 72934cae..9b4b0aba 100644 --- a/src/jquery-ias.js +++ b/src/jquery-ias.js @@ -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; @@ -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 + }); }; /** @@ -652,6 +659,7 @@ next: '.next', pagination: false, delay: 600, - negativeMargin: 10 + negativeMargin: 10, + cacheAjaxResults: true }; })(jQuery);