I am following this railscast in trying to implement an infinite scroll page on my rails app. When a user scrolls down to the bottom of the page an new set of items is appended and the page extends, however it is appended to the page multiple times and the event triggers again on scrolldown even when all the items in the array have been loaded, appending the same set of items again multiple times.
What I would like is to append the 'next page' of items each time a user scrolls to the bottom, and subsequent next pages as the user scrolls to the bottom again.
Here is the jQuery for this function:
jQuery ->
if $('.pagination').length
$(window).scroll ->
url = $('.pagination .next_page').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
$('.pagination').text('Fetching more products...')
$.getScript(url)
$(window).scroll()
and here is the corresponding javascript
$('#products').append('<%= j render(@products) %>');
<% if @products.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@products) %>');
<% else %>
$('.pagination').remove();
<% end %>