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

1

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

Nadianadine answered 7/8, 2015 at 14:41 Comment(1)
An example would be great to work on. i.e. JSFiddleMonohydroxy
C
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');
        }
    }
});
Cailly answered 27/10, 2015 at 17:0 Comment(2)
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.Nadianadine
Awesome! Glad I could helpCailly

© 2022 - 2024 — McMap. All rights reserved.