Rails和编程初学者。
我正在尝试从Railscast 114实现无限页面,但它没有加载。 Firebug控制台出现错误:
document.observe不是函数 - document.observe('dom:loaded', checkScroll);
尝试搜索除了没有加载Prototype之外的原因,但HTML显示它已加载。还尝试在另一个答案中建议的控制器中添加格式部分,但它似乎不起作用。
代码与Ryan有点不同,因为我是从Feed中渲染的。
_feed.html.erb
<% if @feed_items.any? %>
<%= javascript_include_tag :defaults, 'endless_page', 'prototype' %>
<div id="promotions" summary="Status feed">
<%= render :partial => 'shared/feed_item', :collection => @feed_items %>
</table>
<% end %>
pages_controller.rb
class PagesController < ApplicationController
def home
@title = "Home"
if signed_in?
@promotion = Promotion.new
@feed_items = current_user.feed.paginate(:page => params[:page])
respond_to do |format|
format.js
format.html
end
end
end
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);
feed.js.rjs
page.insert_html :bottom, :promotions, :partial => @feed_items
if @feed_items.total_pages > @feed_items.current_page
page.call 'checkScroll'
else
page[:loading].hide
end