Skip to content

Instantly share code, notes, and snippets.

@vincentopensourcetaiwan
Created August 17, 2012 05:50
Show Gist options
  • Save vincentopensourcetaiwan/3376284 to your computer and use it in GitHub Desktop.
Save vincentopensourcetaiwan/3376284 to your computer and use it in GitHub Desktop.
Endless Page
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 posts...")
$.getScript(url)
$(window).scroll()
# /app/assets/javascripts/posts.js.coffee
<%= post.body %><br />
<!-- /app/views/posts/_post.html.erb -->
<h1>Listing posts</h1>
<div id="posts">
<%= render @posts %>
</div>
<%= will_paginate(@posts)%>
<!-- /app/views/posts/index.html.erb -->
$('#posts').append('<%= j render(@posts) %>');
<% if @posts.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@posts) %>');
<% else %>
$('.pagination').remove();
<% end %>
// /app/views/index.js.erb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment