Rails とプログラミングの初心者はこちら。
Railscast 114 からエンドレス ページを実装しようとしていますが、読み込まれません。Firebug コンソールに次のエラーが表示されます。
document.observe は関数ではありません - document.observe('dom:loaded', checkScroll);
プロトタイプがロードされていない以外の理由を検索しようとしましたが、HTML はロードされていることを示しています。また、別の回答で提案されているコントローラーにフォーマット部分を追加しようとしましたが、うまくいかないようです。
フィードからレンダリングしているため、コードは Ryan のものとは少し異なります。
_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