从 Railscast 114 实现无限页面时,获取 document.observe 不是一个函数
Rails 和编程初学者在这里。
我正在尝试从 Railscast 114 实现无限页面,但它没有加载。 Firebug 控制台出现错误:
document.observe 不是一个函数 - document.observe('dom:loaded', 检查滚动);
已尝试寻找除了未加载 Prototype 之外的原因,但 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 %>
页面_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
无尽_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
Rails and programming beginner here.
I'm trying to implement endless page from Railscast 114 but it isn't loading. Firebug console brings up an error:
document.observe is not a function - document.observe('dom:loaded',
checkScroll);
Have tried searching for reasons why other than not having Prototype loaded, but the HTML shows that it is loaded. Also tried adding the format part in the controller suggested in another answer but it doesn't seem to work.
The code is a bit different from Ryan's as I'm rendering from a 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论