简体   繁体   English

Rails:在filterrific之后加载Jquery Infinite Pages

[英]Rails: Jquery Infinite Pages not loading after filterrific

I'm using Rails, jquery infinite pages , kaminari, and filterrific to allow users to filter and infinite scroll through a list of movie trailers. 我正在使用Rails, jquery无限页面 ,kaminari和filterrific来允许用户过滤和无限滚动电影预告片列表。

The infinite scroll works by waiting until the user scrolls within X pixels of the pagination div, then it adds the next X trailers to a parent div using AJAX. 无限滚动通过等待,直到用户在分页div的X像素内滚动,然后使用AJAX将下一个X预告片添加到父div。

<div class="pagination">
  <a rel="next" data-remote="true" href="link-to-next-page"></a>
</div>

The infinite scroll is working perfect when the page first loads. 页面首次加载时,无限滚动工作正常。 BUT if the user filters first, then the infinite scroll no longer works. 但是如果用户首先过滤,则无限滚动不再起作用。 Here's the JS that is getting called when a user filters: 这是用户过滤时调用的JS:

<% js = escape_javascript(
  render 'trailers/list', trailers: @trailers
) %>

$("#filterrific_results").html("<%= js %>");
$(".infinite-table").append('<div class="pagination"><%=j link_to_next_page(@trailers, '', :remote => true) %></div>')

and the html... 和HTML ...

<div class="infinite-table">

  <div class="trailers text-center" id="filterrific_results">
    <div class="trailers-container">
      <%= render 'trailers/list', trailers: trailers %>
    </div>
    <div class="pagination">
      <%= link_to_next_page(trailers, '', :remote => true) %>
    </div>
  </div>

  <div class="clearfix"></div>


</div>

trailers/_list.html.erb: 拖车/ _list.html.erb:

<% trailers.each_with_index do |trailer, index| %>
  <%= render "/trailers/trailer", trailer:trailer, index: index %>
<% end %>

After the user filters, I can see the first set of filtered trailers are loading into the trailers-container as expected. 在用户过滤后,我可以看到第一组过滤后的预告片正按预期加载到预告片容器中。 I can also see that the $(".pagination") is present. 我还可以看到$(“。pagination”)存在。

But for some reason, when I scroll to the bottom of the page... the infinite scroll is not working. 但出于某种原因,当我滚动到页面底部时......无限滚动无效。

My gut tells me it has something to do with the way I've loaded the pagination div dynamically, maybe it's not properly binded and therefore has no event listener on it? 我的直觉告诉我它与我动态加载分页div的方式有关,也许它没有正确绑定,因此没有事件监听器?

Any ideas? 有任何想法吗?

From our understanding working on a very similar issue, your assessment of the element binding is correct. 根据我们对非常类似问题的理解,您对元素绑定的评估是正确的。 When filterrific_results is loaded, the elements are completed rewritten and in this context, the binding is not known (it was done most likely only at page load). 加载filterrific_results时,元素完成重写,在此上下文中,绑定未知(最有可能仅在页面加载时完成)。 We fixed this (in a somewhat inelegant manner) by rebinding when index.js.erb updates the div as below (note that 'toggle' is our problem equivalent of infinite-table): 当index.js.erb更新div时,我们通过重新绑定修复了这个(以一种不太优雅的方式)(请注意'toggle'是我们的问题,相当于无限表):

<% js = escape_javascript( render(partial: 'enrollments/list', locals: { enrollments: @enrollments }) ) %>
$("#filterrific_results").html("<%= js %>");
console.log('after filterrific trigger');
$('.toggle')
    .bind('change', toggle_change)
    .bind('blur', toggle_blur);

Addenda: note that toggle_change & toggle_blur functions MUST be in global scope. 附录:请注意,toggle_change和toggle_blur函数必须在全局范围内。 We modified our coffee script accordingly such that these methods would be available. 我们相应地修改了咖啡脚本,以便可以使用这些方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM