看了 railscasts 的一个视频 Endless Page,想自己实现一个。可是怎么也跑不起来。
#controller
def index
@products = Product.paginate(page: params[:page],per_page: 15)
end
#view /index.js.rjs
page.insert_html :bottom, :products, :partial => 'products'
if @products.total_pages > @products.current_page
page.call 'checkScroll'
else
page[:loading].hide
end
#assets/javascript/endless_page.js
var currentPage = 1;
function checkScroll() {
if (nearBottomOfPage()) {
currentPage++;
new Ajax.Request('/products.js?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);
这里还出现了js错误
##Uncaught TypeError: undefined is not a function
关于这个加载更多的应该如何实现的吗?