Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing a endless scroll with Rails 3 and Jquery
    primarykey
    data
    text
    <p>I'm trying to implementing an endless scroll on my project. I'm using a mix of the Railscast <a href="http://railscasts.com/episodes/114-endless-page" rel="noreferrer">#114 Endless Page</a> and <a href="https://github.com/amatsuda/kaminari/wiki/How-To:-Create-Infinite-Scrolling-with-jQuery" rel="noreferrer">this</a>.</p> <p>Everything works fine besides a weird behavior when I try to stop sending requests when the page hits its end. </p> <p>So far I have:</p> <p>Controller:</p> <pre><code>def show @title = Photoset.find(params[:id]).name @photos = Photoset.find(params[:id]).photo.paginate(:page =&gt; params[:page], :per_page =&gt; 20) respond_to do |format| format.js format.html end end </code></pre> <p>Show.html.erb: </p> <pre><code>&lt;% content_for :body_class, '' %&gt; &lt;%= render 'shared/header' %&gt; &lt;div id="photos_container"&gt; &lt;div id="photos_header"&gt; &lt;h2&gt;&lt;%= @title %&gt;&lt;/h2&gt; &lt;/div&gt; &lt;%= render :partial =&gt; 'photo', :collection =&gt; @photos %&gt; &lt;/div&gt; &lt;%= render :partial =&gt; 'endless_scroll' %&gt; </code></pre> <p>Javascript (loaded via partial):</p> <pre><code>&lt;script type="text/javascript"&gt; (function() { var page = 1, loading = false, finish = false; function nearBottomOfPage() { return $(window).scrollTop() &gt; $(document).height() - $(window).height() - 200; } function finish() { finish = true; } $(window).scroll(function(){ if (loading) { return; } if(nearBottomOfPage() &amp;&amp; !finish) { loading=true; page++; $.ajax({ url: '/photosets/&lt;%= params[:id] %&gt;?page=' + page, type: 'get', dataType: 'script', success: function() { loading=false; } }); } }); }()); &lt;/script&gt; </code></pre> <p>show.js.erb</p> <pre><code>$("#photos_container").append("&lt;%= escape_javascript(render :partial =&gt; 'photo', :collection =&gt; @photos) %&gt;"); &lt;% if @photos.total_pages == params[:page].to_i() %&gt; page.call 'finish' &lt;% end %&gt; </code></pre> <p>As you can see, on my <code>show.js.erb</code> I have a <code>page.call</code> that assigns true to the <code>finish</code> variable. This stops the requests.</p> <p>The wired thing is that it never loads the last page. When <code>@photos.total_pages == params[:page].to_i()</code> instead of just calling the <code>finish</code> function and setting the variable to <code>true</code>, it's also preventing the <code>$("#photos_container").append("&lt;%= escape_javascript(render :partial =&gt; 'photo', :collection =&gt; @photos) %&gt;");</code> from running.</p> <p>It sends the request to the controller, runs the SQL but doesn't append the last page.</p> <p>If I change the condition to <code>@photos.total_pages &lt; params[:page].to_i()</code> it works, but send an extra request to a page that doesn't exist.</p> <p>I'd appreciate any help on my implementation. I'm not sure if there's a more adequate (Rails) way to accomplish this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload