infinite-scroll
infinite-scroll copied to clipboard
checkLastPage not working when retrieving JSON
Hello, I'm using Infinite Scroll with this code :
$container.infiniteScroll({
path: function() {
var pageIndex = this.pageIndex + 1;
var url = jQuery('#posts').data('url');
url = url+pageIndex;
return url;
},
append: false,
responseType: 'text',
});
$container.on( 'load.infiniteScroll', function( event, response ) {
var data = JSON.parse( response );
$container.append( data.content );
});
But, when "/getLastPost" url is returning a blank JSON, infinite scroll is not detecting that empty result and is still trying to load the next page.
How can I fix this ?
One solution is to set scrollThreshold: false
when there is no more content to append.
$container.on( 'load.infiniteScroll', function( event, response ) {
if ( !response || !Object.keys( response ).length ) {
// disable scroll load behavior if no response
$container.data('infiniteScroll').scrollThreshold = false;
return;
}
var data = JSON.parse( response );
$container.append( data.content );
});
Event when my path function returns nothing. It still tries to fetch next page with path as "undefined"
page-load.js:55 GET http://localhost:3000/undefined 404 (Not Found)