0

我正在使用 Rails 和 Kaminari,并试图在 Locations 和 Posts 下方的评论上实现无休止的滚动。我一开始是对的,但是当我向下滚动时,它会一遍又一遍地加载相同的评论。如何使它正确加载评论?

这是我的位置/show.js.erb

  1 $('#comments').append('<%= j render(@comments) %>');

这是我的comments.js

  5 jQuery ->
  6   $(window).scroll ->  
  7     if $(window).scrollTop() > $(document).height() - $(window).height() - 200
  8       $.getScript($('.pagination .next_page a').attr('href'))

我的评论控制器显示动作

 18   def show
 19     @title = @location.name
 20     @comments = @location.comments.order("created_at desc").page(params[:page]).per(35)
 21     @commentable = @location
 22     @comment = @location.comments.build
 23     respond_to do |format|
 24       format.html
 25       format.js
 26     end
 27   end

编辑:这是分页html源

<div class="pagination">
    <ul>

          <li class="active">
  <a href="#">1</a>
</li>

          <li class="">
  <a href="/locations/1?page=2" rel="next">2</a>
</li>

          <li class="">
  <a href="/locations/1?page=3">3</a>
</li>

          <li class="">
  <a href="/locations/1?page=4">4</a>
</li>

          <li class="">
  <a href="/locations/1?page=5">5</a>
</li>

      <li>
  <a href="/locations/1?page=5">Last &raquo;</a>
</li>

    </ul>
  </div>
4

3 回答 3

1

.next_page 链接没有改变。链接就是whatever.com/?page=2。当您滚动时,它只是对同一页面发出请求。你要么需要你的 javascript 来更新链接中的页码,要么不像那样引用 DOM。

您可以使用以下命令更新分页器:

$('.pagination').html('<%= escape_javascript(paginate(@comments)) %>');

Kaminari 对此有一个“操作方法”:操作方法:-Create-Infinite-Scrolling-with-jQuery

于 2013-02-16T16:01:04.160 回答
1

在您的回调中,$.getScript您可以通过以下方式增加页码next

jQuery ->
    $(window).scroll ->  
      if $(window).scrollTop() > $(document).height() - $(window).height() - 200
        var $next=$('.pagination .next_page a')
         $.getScript($next.attr('href'), function(){
              /* script has loaded*/
              $next.attr('href', function(i, currHref){
                 return currHref.replace(/(?!page\=)(\d)/, function(match){
                        return parseInt(match,10)+1;
                   });
              });
         });
于 2013-02-16T16:53:35.667 回答
0

好的,我发现了问题所在。感谢帮助过的人,非常感谢。这是有效的代码。(对不起,如果这些评论打扰了你,但我决定把它们留在这里,以防有人想纠正我或者他们将来帮助别人)。

位置.js.coffee

  5 # Infinite scroll
  6 jQuery ->
  7   # it doesn't listen for the scroll unless there is location_comments class
  8   if $('.location_comments').length
  9     $(window).scroll ->
 10       # finds anchor tag in pagination with rel attribute = 'next'
 11       $next = $(".pagination a[rel='next']")
 12       # sets url variable to the href value of that anchor tag
 13       url = $($next).attr('href')
 14       if url && $(window).scrollTop() > $(document).height() - $(window).height() - 200
 15         # since this replaces whole pagination with this text, it prevents loading of too many records.
 16         # This line ise immediatly followed by this getScript() wich triggers another operation on .pagination
 17         $('.pagination').text 'Fetching more comments...'
 18         # triggers /locations/show.js.erb
 19         $.getScript(url)
 20     $(window).scroll

位置/show.js.erb

  1 $('.location_comments').append('<%= j render @comments %>');
  2 $('.pagination').replaceWith('<%= j paginate @comments %>');
  3 /* Add code for removing pagination if the user is on last page */

评论控制器

  9   def create
 10     @comment = @commentable.comments.build(params[:comment])
 11     @comment.user = current_user
 12 
 13     respond_to do |format|
 14       if @comment.save
 15         format.html { redirect_to @commentable, :notice => 'Comment created' }
 16         format.js
 17       else
 18         format.html { redirect_to @commentable, :alert => 'Comment not created' }
 19         format.js
 20       end
 21     end

位置控制器

 18   def show
 19     @title = @location.name
 20     @comments = @location.comments.order("created_at desc").page(params[:page]).per(35)
 21     @commentable = @location
 22     @comment = @location.comments.build
 23   end

当用户在最后一页时,我仍然需要找出如何隐藏 .pagination ,所以如果有人想提供帮助,那就太棒了。

于 2013-02-19T23:41:58.637 回答