Skip to content

Commit

Permalink
set up endless scroll successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
KelseyDH committed Feb 2, 2015
1 parent b46a95e commit 554538f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@
//= require jquery_ujs
//= require turbolinks
//= require_tree .

$(document).ready(function() {
if ($('.pagination').length) {
$(window).scroll(function() {
var url = $('.pagination .next_page').attr('href');
if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
$('.pagination').text("Please Wait...");
return $.getScript(url);
}
});
return $(window).scroll();
}
});
2 changes: 2 additions & 0 deletions app/views/posts/index.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$('#posts').append('<%= escape_javascript render(@posts) %>');
$('.pagination').replaceWith('<%= escape_javascript will_paginate(@posts) %>');
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
#
(1..100).each do |i|
(1..10000).each do |i|

This comment has been minimized.

Copy link
@BenMorganIO

BenMorganIO Feb 2, 2015

That's a lot @KelseyDH.

This comment has been minimized.

Copy link
@KelseyDH

KelseyDH Feb 2, 2015

Author Owner

Yeah I forgot my SSD drive is not the typical performance of most rigs :P I've since lowered it.

This comment has been minimized.

Copy link
@BenMorganIO

BenMorganIO Feb 2, 2015

You can speed it up by wrapping it in an ActiveRecord::Base.transaction. Cheap trick...

Or:

post_inserts = []
10000.times do |i|
  post_insert = []
  post_insert << "'Lorem ipsum #{i}'"
  post_insert << "'Lorem ipsum dolor sit amet.'"
  post_inserts << post_insert
end

connection = ActiveRecord::Base.connection
connection.execute <<-SQL
  INSERT INTO posts ("title", "body")
  VALUES (#{post_inserts.map { |v| v.join(", ") }.join("), (")});
SQL

I always do that with large seeds.

Post.create!(title: "Lipsum Post #{i}", body: %{
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In feugiat purus dapibus fermentum sagittis. Fusce in tempus felis. Phasellus a erat ut lorem lacinia bibendum. Vivamus viverra facilisis neque, in scelerisque urna pharetra vel. Donec a est mauris. Integer eget metus quis eros egestas elementum. Integer bibendum risus hendrerit dapibus tempor. Fusce placerat in orci vitae tincidunt.
})
Expand Down

0 comments on commit 554538f

Please sign in to comment.