11

I'm using the infinitescroll.js script and it works really well. I've found out how to replace the default functionality with a load more button, using this code:

$(window).unbind('.infscr');
$('.js-next-reports').click(function() {
    $grid.infinitescroll('retrieve');
    return false;
});
$(document).ajaxError(function(e, xhr, opt) {
    if (xhr.status == 404) $('.js-next-reports').remove();
});

However, what I'd like to do is allow infinite scroll to run 3/4 times and then show the .js-next-reports button. I'm not sure how to keep track of how many times infinite scroll has run though. I know there is a currPage var but using console.log I can't work out how I can reference it.

There is also a maxPage option for infinitescroll, which limits it to run only X times, so I could maybe tap into that somehow? I'm not sure how to get a console.log of the options though. Here is my init code if that helps ($grid is just a ref to a div)

$grid.infinitescroll({

    // selector for the paged navigation (it will be hidden)
    navSelector  : ".pagination",
    // selector for the NEXT link (to page 2)
    nextSelector : ".pagination .next",
    // selector for all items you'll retrieve
    itemSelector : ".infinite-scroll-post",
    contentSelector : "#infinite-scrollable",
    debug: true,

    // finished message
    loading: {
        img: "ajax-loader.gif",
        msgText: "Loading more projects...",
        finishedMsg: 'No more pages to load.',
        }
    },

});

Maybe something like: ?

if ( .currPage == "3" ) {
    $(window).unbind('.infscr');
    $('.js-next-reports').click(function() {
        $grid.infinitescroll('retrieve');
        return false;
    });
    $(document).ajaxError(function(e, xhr, opt) {
        if (xhr.status == 404) $('.js-next-reports').remove();
    });
}

But I don't know how to either count the scrolls or access currPage.

Thanks

Community
  • 1
  • 1
patrickzdb
  • 838
  • 1
  • 9
  • 24

1 Answers1

3

A JSFiddle would help testing the code, but from what I've read on their documentation, there's a callback that allows you to access currPage inside the state object. Your code should look something like this:

$grid.infinitescroll({

    // selector for the paged navigation (it will be hidden)
    navSelector  : ".pagination",
    // selector for the NEXT link (to page 2)
    nextSelector : ".pagination .next",
    // selector for all items you'll retrieve
    itemSelector : ".infinite-scroll-post",
    contentSelector : "#infinite-scrollable",
    debug: true,

    // finished message
    loading: {
        img: "ajax-loader.gif",
        msgText: "Loading more projects...",
        finishedMsg: 'No more pages to load.',
    },
    appendCallback: false
    }, function(newitems, opts) {
        if(opts.state.currPage == 3) {
            $(window).unbind('.infscr');
        }
    }
});
Lucas Rodrigues
  • 1,226
  • 2
  • 10
  • 22
  • Thanks. I already had a different callback so I had to include `opts` into that and then add the if statement into it, but thanks for getting me on the right path. You also took out my extra `}` from the `loading` option so cheers for that - I was wondering why I was getting errors. – patrickzdb Nov 03 '15 at 15:19
  • Awesome! Glad I could help – Lucas Rodrigues Nov 03 '15 at 15:22