1

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

无尽的页面.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
4

0 回答 0