从 Railscast 114 实现无限页面时,获取 document.observe 不是一个函数

发布于 2025-01-06 12:57:48 字数 1850 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文