From 906b7978d6cff05f7bd872419f346ddd91ff738f Mon Sep 17 00:00:00 2001 From: ve3 Date: Wed, 4 May 2022 09:51:14 +0700 Subject: [PATCH] v0.0.5 * Fix check for calling XHR before make AJAX call to prevent multiple scroll events cause fail events `reject('previous XHR is calling.')`. --- package.json | 2 +- src/RundizScrollPagination.js | 72 ++++++++++++++++++----------------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index ad4a84a..d42d9aa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rundiz-scroll-pagination", - "version": "0.0.4", + "version": "0.0.5", "description": "Scroll down the page and automatically call to get next page contents.", "main": "index.js", "directories": { diff --git a/src/RundizScrollPagination.js b/src/RundizScrollPagination.js index 3a0ce70..02bd272 100644 --- a/src/RundizScrollPagination.js +++ b/src/RundizScrollPagination.js @@ -3,7 +3,7 @@ * * @author Vee W. * @license MIT. - * @version 0.0.3 + * @version 0.0.5 */ @@ -317,43 +317,45 @@ class RundizScrollPagination { } );*/ - // begins ajax pagination. - this.ajaxPagination() - .then((responseObject) => { - responseObject.rdScrollPaginationCurrentPageOffset = thisClass.previousStartOffset; - document.dispatchEvent( - new CustomEvent( - 'rdScrollPagination.done', {'detail': responseObject} - ) - ); - // trigger on scroll for in case that there are space left in the bottom of visible area. - // trigger will try to load next ajax page if there are more space left. - thisClass.triggerOnScroll(); - - return Promise.resolve(responseObject); - }) - .catch((responseObject) => { - // .catch() must be after .then(). see https://stackoverflow.com/a/42028776/128761 - if (typeof(responseObject) === 'object') { - // if responseObject is not an object. - // due to ajax error can throw a string message, check before assign. + if (thisClass.callingXHR === false) { + // begins ajax pagination. + this.ajaxPagination() + .then((responseObject) => { responseObject.rdScrollPaginationCurrentPageOffset = thisClass.previousStartOffset; - } - - document.dispatchEvent( - new CustomEvent( - 'rdScrollPagination.fail', {'detail': responseObject} - ) - ); + document.dispatchEvent( + new CustomEvent( + 'rdScrollPagination.done', {'detail': responseObject} + ) + ); + // trigger on scroll for in case that there are space left in the bottom of visible area. + // trigger will try to load next ajax page if there are more space left. + thisClass.triggerOnScroll(); + + return Promise.resolve(responseObject); + }) + .catch((responseObject) => { + // .catch() must be after .then(). see https://stackoverflow.com/a/42028776/128761 + if (typeof(responseObject) === 'object') { + // if responseObject is not an object. + // due to ajax error can throw a string message, check before assign. + responseObject.rdScrollPaginationCurrentPageOffset = thisClass.previousStartOffset; + } - return Promise.reject(responseObject) - .then(() => { - // not called. - }, (responseObject) => { - // prevent uncaught error. + document.dispatchEvent( + new CustomEvent( + 'rdScrollPagination.fail', {'detail': responseObject} + ) + ); + + return Promise.reject(responseObject) + .then(() => { + // not called. + }, (responseObject) => { + // prevent uncaught error. + }); }); - }); - } + }// endif callingXHR check + }// endif; total scroll > document scroll height. }// checkScrollAndMakeXHR