0

我正在关注这个railcasts以在我的 rails 应用程序中集成无限滚动。我在这里这样做..

static_pages_controller.rb --

def home
  @posts = Post.paginate(page: params[:page],:per_page => 10)
end

home.html.erb --

<div id="posts_list" style="height:300px; overflow:scroll">
   <%= render :partial => "posts/posts" %>
   <p id="loading">Loading more page results...  </p>
</div>

部分帖子/帖子 ---

<% if @posts.any? %>
  <div id="postlist">
  <ul>
    <%= render partial: 'posts/posts_item', collection: @posts %>
  </ul>
  </div>
<% end %>

部分帖子/posts_item --

<li><%= posts_item.title %></li>
<li><%= posts_item.content %></li>

我的 static_pages/home.js.erb--

page.insert_html :bottom, :posts, :partial => @posts
  if @posts.total_pages > @posts.current_page
   page.call 'checkScroll'
  else
   page[:loading].hide
  end

和我的资产/javascripts/endless_page.js——

var currentPage = 1;

 function checkScroll() {
   if (nearBottomOfPage()) {
    currentPage++;
    new Ajax.Request('/?page=' + currentPage, {asynchronous:true, evalScripts:true, method:'get'});
   }    else {
     setTimeout("checkScroll()", 250);
  }
}

function nearBottomOfPage() {
  return scrollDistanceFromBottom() < 150;
}

function scrollDistanceFromBottom(argument) {
  return pageHeight() - (window.pageYOffset + self.innerHeight);  
}

function pageHeight() {
  return Math.max(document.body.scrollHeight, document.body.offsetHeight);
}

document.observe('dom:loaded', checkScroll);

但我无休止的滚动不起作用。我不知道我在哪里做错了。请帮忙。

4

0 回答 0