【问题标题】:rails infinite scroll repeating contentsrails无限滚动重复内容
【发布时间】:2014-06-27 22:49:03
【问题描述】:

我不确定我在这里缺少什么。我已经在页面上实现了无限滚动,但结果被重复了不止一次。我为我的开发环境创建了一个项目列表,这些项目是按顺序制作的。它们在无限卷轴中被重复,例如。项目编号 49、48、47、46、45、44、46、45、44、46、45、44、43,......

index.js.erb

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

index.html.erb

<section class='page-content-container'>
 <h2 class='static-page-subheading'>Stay Up to Date</h2>
  <%= render 'news' %>
   <div id="infinite-scrolling">
    <%= will_paginate @news%>
   </div>
 </section> 

_news.html.erb

<div id='my-posts'>
 <% @news.each do |news| %>
  <div class='news-item-container'>
<h2><%= news.title %></h2>
    <h3><%= news.created_at %></h3>
<%= truncate(news.body, :length => 500, escape: false) %>
<%= link_to 'Read More', news %>
<div class="addthis_sharing_toolbox"></div>
   </div>
  <% end %>
  </div>

pagination.js.coffee (此处忽略缩进)

 jQuery ->
   if $('#infinite-scrolling').size() > 0
     $(window).on 'scroll', ->
       more_news_url = $('.pagination .next_page a').attr('href')

       if more_news_url && $(window).scrollTop() > $(document).height() - $(window).height() - 60
         $('.pagination').html('<img src="/assets/ajax-loader.gif" alt="Loading..." title="Loading..." />')
         $.getScript more_news_url
         return
       return

【问题讨论】:

  • 不,它只是堆栈溢出。稍等,我会编辑
  • 我可以看看你的控制器吗?

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


【解决方案1】:

刚刚在本地复制它,我记得我以某种方式修复了这个问题,现在我记得了。这就是我修复它的方法:

index.html.erb

<%= render partial: "posts" %>
<div class="append-me"></div>

<%= will_paginate @posts, class: 'pagination pagination-sm' %>

index.js.erb

$('.append-me').append( '<%= j render "posts" %>' );


<% if @posts.next_page %>
    $('.pagination').replaceWith('<%= j will_paginate @posts, class: "pagination pagination-sm" %>');
<% else %>
    $('.pagination').remove();
<% end %>

_posts.html.erb

<% @posts.each do |post| %>
<div class="well" style="margin: 15% 0%;">
    <%= post.data %>
</div>

<% end %>

post.js

if ($('.pagination')) {
         $(window).scroll(function() {
            console.log("sdasd")
            url = $(' .next_page').attr('href');

            // console.log($(window).scrollTop())
            // console.log($(document).height())
            // console.log($(document).height() - $(window).height() - 50)
            if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 150) {

                $('.pagination').text("Fetching more profiles...");
                $.getScript(url)
            }
            // $(window).scroll();
        });
    }

诀窍是,我必须明确地在我的 index.html 和 index.js 文件中创建类“分页”。其次,我为我的 index.js.erb 文件做了&lt;%= j render "posts" %&gt; 而不是@posts

【讨论】:

  • 感谢您的帮助。但是,我已经完全复制了这个并适应了“新闻”,但仍然无法正常工作。我可以在控制台中看到 js 正在工作,但没有无限滚动。这正是你所做的吗?
  • 是的,你上课了吗
  • 是的,我猜不出来
  • 尝试删除这个$(window).off('scroll');
猜你喜欢
  • 2012-10-31
  • 1970-01-01
  • 2020-03-09
  • 2014-04-08
  • 2018-05-21
  • 1970-01-01
  • 2015-04-01
  • 1970-01-01
  • 2021-10-15
相关资源
最近更新 更多