I am following this railscast , trying to implement an endless scroll page in my rails application. When the user scrolls to the bottom of the page, a new set of elements is added, and the page expands, however it is added to the page several times, and the event is fired again on scrolldown, even when all elements in the array are loaded, adding the same set of elements repeatedly several times .
I would like to add a “next page” of elements each time the user scrolls to the bottom and subsequent pages when the user scrolls down again.
Here is 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 %>
source share