jQuery无尽页面与will_paginate不工作在Rails [英] jQuery Endless page with will_paginate not working in Rails

查看:127
本文介绍了jQuery无尽页面与will_paginate不工作在Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现Railscasts第114集中显示的无尽的页面功能。分页效果很好,但是无尽的页面功能根本不会触发。我没有看到任何错误;只是分页,好像我没有添加无尽的页面javascript。我的代码:

I'm trying to implement the endless page functionality shown in Railscasts episode 114. The pagination works great but the endless page functionality doesn't trigger at all. I don't see any errors; just the pagination as if I didn't add the endless page javascript. My code:

活动控制器

class ActivitiesController < ApplicationController
    before_filter :authenticate_user!
  def index
    @activities = PublicActivity::Activity.order("created_at DESC").where(owner_type: "User", owner_id: current_user.followed_users.map {|u| u.id}).page(params[:page]).per_page(15)
    @post = current_user.posts.build

     respond_to do |format|
      format.html
      format.json
      format.js 
    end
  end
end

activities.js.coffee

activities.js.coffee

jQuery ->
  if $('.pagination').length
    $(window).scroll ->
      url = $('.pagination .next_page').attr('href')
      if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
        $('.pagination').text("Loading more activities...")
        $.getScript(url)
    $(window).scroll()

index.js.erb



index.js.erb

$('#activities').append('<%= j render ('shared/activities') %>');
<% if @activities.next_page %>
  $('.pagination').replaceWith('<%= j will_paginate('shared/activities') %>');
<% else %>
  $('.pagination').remove();
<% end %>
<% sleep 1 %>

共享/ _activities.html.erb

shared/_activities.html.erb

<div class="activities">
<% @activities.each do |activity| %>
<code>
<% end %>
<%= will_paginate @activities %>
</div>

问题必须是javascript,但我不能弄清楚。关于可能导致问题的任何想法?

The issue must be with the javascript but I can't figure it out. Any ideas as to what could be causing the issue?

谢谢!
-b

Thanks! -b

推荐答案

我设法在Rails 4中没有一个index.js.erb文件。只需添加这个coffeescript:

I managed to do it withOUT an "index.js.erb" file in Rails 4. Just add this coffeescript:

$(window).on 'scroll', ->
  if $('.pagination').length
    @url = $('.pagination .next_page').attr('href')
    if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
      $('.pagination').remove()
      $('#articles').append('<div>')
      $('#articles div').last().load @url+' #articles', ->
          if $('.next_page.disabled').length
            $('.pagination').remove()

这将加载下一页,然后在新加载的页面上使用分页,直到没有更多的下一页。只需用脚本中的容器的id替换#articles即可。跳过index.js.erb和respond_to jazz。

This loads the next page then uses the pagination on the newly loaded page until there are no more next pages. Just replace "#articles" with the id of your container in the script. Skip the index.js.erb and respond_to jazz.

这篇关于jQuery无尽页面与will_paginate不工作在Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆