Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checkLastPage not working when retrieving JSON #906

Open
Snoop9 opened this issue Jan 3, 2020 · 2 comments
Open

checkLastPage not working when retrieving JSON #906

Snoop9 opened this issue Jan 3, 2020 · 2 comments

Comments

@Snoop9
Copy link

Snoop9 commented Jan 3, 2020

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 ?

@desandro
Copy link
Member

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 );
});

@kabirsaheb
Copy link

kabirsaheb commented May 14, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants