【问题标题】:Rails & JS : Infinite ScrollingRails & JS:无限滚动
【发布时间】:2015-04-01 21:57:15
【问题描述】:

我已关注this tutorial 以无限滚动转换我的索引页面。

但结果是我有一些分页按钮而不是无限滚动

这是我的代码:

index.html.erb

-

<div id="my-annonces">
     <%= render @annonces %>
</div>
<div id="infinite-scrolling">
     <%= will_paginate %>
</div>

-

AnnoncesController

-

class AnnoncesController < ApplicationController
 def index
  page_paginate = 6
  @annonces = Annonce.paginate(page: params[:page], per_page: page_paginate).order('created_at DESC')
  respond_to do |format|
   format.html
   format.js
  end
end

-

index.js.erb

-

   $('#my-annonces').append('<%= j render @annonces %>');
    <% if @annonces.next_page %>
       $('.pagination').replaceWith('<%= j will_paginate @annonces %>');
    <% else %>
       $(window).off('scroll');
       $('.pagination').remove();
    <% end %>

-

这个 js 永远不会被调用,现在不知道为什么。如果我把它称为它的视图。但是我不知道.pagination是什么...

-

正如他所说,我在 javascripts 目录中创建了一个新文件 pagination.js.coffee

-

jQuery ->
 if $('#infinite-scrolling').size() > 0
  console.log "AAAAA "
  $(window).on 'scroll', ->
      console.log "BBBBBB "
    more_posts_url = $('.pagination .next_page a').attr('href')
      console.log "CCCCC "
    if more_posts_url && $(window).scrollTop() > $(document).height() - $(window).height() - 60
          console.log "DDDDD "
        $('.pagination').html('<img src="/assets/ajax-loader.gif" alt="Loading..." title="Loading..." />')
        $.getScript more_posts_url
    return
  return

-

但是这个文件也从来没有被调用过。

我在 rails 控制台或 js 控制台中没有任何错误。我在JS方面非常有经验,所以我有点多。

有谁知道问题出在哪里? 谢谢!

【问题讨论】:

    标签: jquery ruby-on-rails coffeescript pagination infinite-scroll


    【解决方案1】:

    我的错,

    我刚刚忘记包含我的 pagination.js.coffee... 我太笨了,对不起大家!

    没关系,我希望我的帖子能帮助其他人使用无限滚动!

    【讨论】:

    • 你能解释一下“忘记包含我的 pagination.js.coffee”是什么意思吗?我浏览了教程并遇到了同样的问题。
    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 2014-02-09
    • 2012-10-11
    • 1970-01-01
    • 2019-03-10
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多