Skip to content

Commit 7e3840e

Browse files
committedJan 1, 2021
v4.0.0; ES2018 refactor; use fetch & Promise
1 parent 82910c6 commit 7e3840e

5 files changed

+20
-14
lines changed
 

‎dist/infinite-scroll.pkgd.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Infinite Scroll PACKAGED v4.0.0-beta.0
2+
* Infinite Scroll PACKAGED v4.0.0
33
* Automatically add next page
44
*
55
* Licensed GPLv3 for open source use
@@ -844,11 +844,10 @@ proto.loadNextPage = function() {
844844
this.isLoading = true;
845845
if ( typeof fetchOptions == 'function' ) fetchOptions = fetchOptions();
846846

847-
// TODO add fetch options
848847
let fetchPromise = fetch( path, fetchOptions ).then( ( response ) => {
849848
if ( !response.ok ) {
850849
let error = new Error( response.statusText );
851-
this.onPageError( error, path );
850+
this.onPageError( error, path, response );
852851
return { response };
853852
}
854853

@@ -866,7 +865,7 @@ proto.loadNextPage = function() {
866865
} );
867866
} );
868867

869-
this.dispatchEvent( 'request', null, [ path ] );
868+
this.dispatchEvent( 'request', null, [ path, fetchPromise ] );
870869

871870
return fetchPromise;
872871
};
@@ -883,18 +882,25 @@ proto.onPageLoad = function( body, path, response ) {
883882
};
884883

885884
proto.appendNextPage = function( body, path, response ) {
886-
let { append, responseBody } = this.options;
885+
let { append, responseBody, domParseResponse } = this.options;
887886
// do not append json
888-
let isDocument = responseBody == 'text';
887+
let isDocument = responseBody == 'text' && domParseResponse;
889888
if ( !isDocument || !append ) return { body, response };
890889

891890
let items = body.querySelectorAll( append );
891+
let promiseValue = { body, response, items };
892+
// last page hit if no items. #840
893+
if ( !items || !items.length ) {
894+
this.lastPageReached( body, path );
895+
return promiseValue;
896+
}
897+
892898
let fragment = getItemsFragment( items );
893899
let appendReady = () => {
894900
this.appendItems( items, fragment );
895901
this.isLoading = false;
896-
this.dispatchEvent( 'append', null, [ body, path, items ] );
897-
return { body, response, items };
902+
this.dispatchEvent( 'append', null, [ body, path, items, response ] );
903+
return promiseValue;
898904
};
899905

900906
// TODO add hook for option to trigger appendReady
@@ -1000,10 +1006,10 @@ proto.lastPageReached = function( body, path ) {
10001006

10011007
// ----- error ----- //
10021008

1003-
proto.onPageError = function( error, path ) {
1009+
proto.onPageError = function( error, path, response ) {
10041010
this.isLoading = false;
10051011
this.canLoad = false;
1006-
this.dispatchEvent( 'error', null, [ error, path ] );
1012+
this.dispatchEvent( 'error', null, [ error, path, response ] );
10071013
return error;
10081014
};
10091015

‎dist/infinite-scroll.pkgd.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎js/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Infinite Scroll v4.0.0-beta.0
2+
* Infinite Scroll v4.0.0
33
* Automatically add next page
44
*
55
* Licensed GPLv3 for open source use

‎package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "infinite-scroll",
3-
"version": "4.0.0-beta.0",
3+
"version": "4.0.0",
44
"description": "Automatically add next page",
55
"main": "js/index.js",
66
"files": [

0 commit comments

Comments
 (0)
Please sign in to comment.