Hey I'm trying to add endless scrolling to a rails app...i followed the tutorial by ryan bates but im still having some trouble getting the next page content to load.
So you get a clue about the structure im using...i have a controller called Static Pages which mainly has static pages plus a few other nonstatic pages...one of those pages is called 'feed' so within the static_pages view i have a feed.html.erb
It's a feed page that shows users updates
here is the controller (static_pages)
def feed
if signed_in?
@update = current_user.updates.build
@feed_items = current_user.feed.page(params[:page]).per_page(5)
else
flash[:error] = "Please sign in."
redirect_to login_url
end
respond_to do |format|
format.js
format.html
end
end
here is the feed html view
<div class="row">
<div class="span8 offset2 settings update">
<div id="feeds">
<%= render partial: "shared/feed_item", collection: @feed_items %>
</div>
<%= will_paginate @feed_items %>
</div>
</div>
Here is my static_pages.js.coffee (from ryan bates)
jQuery ->
if $('.pagination').length
$(window).scroll ->
url = $('.pagination .next_page a').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
$('.pagination').text("Fetching more products...")
$.getScript(url)
$(window).scroll()
and finally here is my feed.js.erb (in the static_pages view folder)
$('#feeds').append("<%= render partial: 'shared/feed_item', collection: @feed_items %>");
<% if @feed_items.current_page < @feed_items.num_pages %>
$('.pagination').replaceWith('<%= will_paginate(@feed_items) %>');
<% else %>
$('.pagination').remove();
<% end %>
does anyone want to take a stab and try to figure out whats going on here? I'm sure it's something easy that im not seeing. (by the way, the paginate works great before i started working on the endless scrolling)
and when i try to access the feed page and scroll to the bottom, the texts (Fetching more products) does get rendered, but the new feed_items do not.
EDIT for trueunlessfalse
Static_pages coffeescript
jQuery ->
if $('.pagination').length
$(window).scroll ->
url = $('.pagination .next_page a').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 5
$('.pagination').text("Hello world!")
$.getScript(url)
$(window).scroll()
feed.html.erb
<div class="row">
<div class="span8 offset2 settings update">
<div id="feeds" data-no-turbolink>
<%= render partial: "shared/feed_item", collection: @feed_items %>
</div>
<%= will_paginate @feed_items %>
</div>
</div>
feed.js.erb
$('#feeds').append("<%= j render partial: 'shared/feed_item', collection: @feed_items %>");
<% if @feed_items.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@feed_items) %>');
<% else %>
$('.pagination').remove();
<% end %>