无尽的页面一遍又一遍地加载相同的记录

时间:2013-02-16 15:45:34

标签: javascript jquery ruby-on-rails

我正在使用Rails和Kaminari,并试图在位置和帖子下面的评论上实现无休止的滚动。我得到了正确的权利,但当我向下滚动时,它会一遍又一遍地加载相同的注释。如何使它正确加载评论?

这是我的 locations / 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>

3 个答案:

答案 0 :(得分:1)

.next_page链接没有变化。 这个链接就是这里的任何内容。当您滚动时,它只是对同一页面发出请求。您需要使用javascript来更新链接中的页码,或者不要像这样引用DOM。

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

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

Kaminari有一个&#39; How To&#39;为此:How-To:-Create-Infinite-Scrolling-with-jQuery

答案 1 :(得分: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;
                   });
              });
         });

答案 2 :(得分:0)

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

<强> locations.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,所以,如果有人想要帮助那些非常棒的话。