Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow infinitescroll.js to run X times, then load more posts

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

like image 661
patrickzdb Avatar asked Aug 07 '15 14:08

patrickzdb


1 Answers

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');
        }
    }
});
like image 179
Lucas Rodrigues Avatar answered Oct 16 '22 19:10

Lucas Rodrigues