Javascript 无休止的页面反复加载相同的记录
我正在使用Rails和Kaminari,并试图在位置和帖子下方的评论上实现无休止的滚动。我的开始是正确的,但当我向下滚动时,它会一次又一次地加载相同的评论。如何使其正确加载注释 这是我的位置/show.js.erbJavascript 无休止的页面反复加载相同的记录,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,我正在使用Rails和Kaminari,并试图在位置和帖子下方的评论上实现无休止的滚动。我的开始是正确的,但当我向下滚动时,它会一次又一次地加载相同的评论。如何使其正确加载注释 这是我的位置/show.js.erb 1 $('#comments').append('<%= j render(@comments) %>'); 1 $('.location_comments').append('<%= j render @comments %>'); 2 $('
1 $('#comments').append('<%= j render(@comments) %>');
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 */
我的评论控制器显示操作
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 »</a>
</li>
</ul>
</div>
-
-
-
-
-
-
下一页链接未更改。
该链接就是which.com/?page=2。当你滚动时,它只是对同一个页面发出请求。您要么需要javascript更新链接中的页码,要么不这样引用DOM
您可以使用以下命令更新分页器:
$('.pagination').html('<%= escape_javascript(paginate(@comments)) %>');
$('.pagination').html('');
Kaminari对此有一个“操作方法:在
$的回调中。getScript
您可以使用以下命令增加下一个的页码:
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;
});
});
});
好的,我发现了问题所在。感谢那些帮忙的人,非常感谢以下是有效的代码。(如果您对这些评论感到不安,很抱歉,但我决定将它们留在这里,以防有人想纠正我或将来帮助他人)
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 $('#comments').append('<%= j render(@comments) %>');
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
我仍然需要找出如何隐藏。当用户在最后一页时,分页,因此,如果有人想帮助这一点,那就太棒了。您检索的脚本中有什么内容?下一步分页的值会发生什么变化?太多未知数您确定page(params[:page])
按预期工作吗?我还认为下一页的事情是导致问题的原因。我想这不会改变。当我在url中手动传递参数时,它会按应该的方式更改@charlietfl您还需要什么代码,我将添加它。显示next
链接的源html,以便查看href
。将需要在脚本加载时对其进行修改。添加了下一个链接的html源代码。如何在脚本中修改它?我知道这一点,但希望不需要额外的依赖项(香肠)就可以做到这一点。“如何”的重要部分是页面++。您需要在某个地方迭代页码。除非您需要用户与之交互的按钮,否则最好使用code.aaah!你能告诉我如何用JS来编辑页面吗?不要对代码做太多的修改?好吧,我已经在这里将其全部转换为普通JS><但它仍然做同样的事情吗?您在不属于它的地方添加了return
,我提供的基本代码工作正常,不确定您的实现中发生了什么可能您加载的脚本对寻呼机做了什么?