1
1
/*!
2
- * Infinite Scroll PACKAGED v4.0.0-beta.0
2
+ * Infinite Scroll PACKAGED v4.0.0
3
3
* Automatically add next page
4
4
*
5
5
* Licensed GPLv3 for open source use
@@ -844,11 +844,10 @@ proto.loadNextPage = function() {
844
844
this . isLoading = true ;
845
845
if ( typeof fetchOptions == 'function' ) fetchOptions = fetchOptions ( ) ;
846
846
847
- // TODO add fetch options
848
847
let fetchPromise = fetch ( path , fetchOptions ) . then ( ( response ) => {
849
848
if ( ! response . ok ) {
850
849
let error = new Error ( response . statusText ) ;
851
- this . onPageError ( error , path ) ;
850
+ this . onPageError ( error , path , response ) ;
852
851
return { response } ;
853
852
}
854
853
@@ -866,7 +865,7 @@ proto.loadNextPage = function() {
866
865
} ) ;
867
866
} ) ;
868
867
869
- this . dispatchEvent ( 'request' , null , [ path ] ) ;
868
+ this . dispatchEvent ( 'request' , null , [ path , fetchPromise ] ) ;
870
869
871
870
return fetchPromise ;
872
871
} ;
@@ -883,18 +882,25 @@ proto.onPageLoad = function( body, path, response ) {
883
882
} ;
884
883
885
884
proto . appendNextPage = function ( body , path , response ) {
886
- let { append, responseBody } = this . options ;
885
+ let { append, responseBody, domParseResponse } = this . options ;
887
886
// do not append json
888
- let isDocument = responseBody == 'text' ;
887
+ let isDocument = responseBody == 'text' && domParseResponse ;
889
888
if ( ! isDocument || ! append ) return { body, response } ;
890
889
891
890
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
+
892
898
let fragment = getItemsFragment ( items ) ;
893
899
let appendReady = ( ) => {
894
900
this . appendItems ( items , fragment ) ;
895
901
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 ;
898
904
} ;
899
905
900
906
// TODO add hook for option to trigger appendReady
@@ -1000,10 +1006,10 @@ proto.lastPageReached = function( body, path ) {
1000
1006
1001
1007
// ----- error ----- //
1002
1008
1003
- proto . onPageError = function ( error , path ) {
1009
+ proto . onPageError = function ( error , path , response ) {
1004
1010
this . isLoading = false ;
1005
1011
this . canLoad = false ;
1006
- this . dispatchEvent ( 'error' , null , [ error , path ] ) ;
1012
+ this . dispatchEvent ( 'error' , null , [ error , path , response ] ) ;
1007
1013
return error ;
1008
1014
} ;
1009
1015
0 commit comments