Permalink
Browse files

implementing startkey pagination on infinite scrolling lists t:100

  • Loading branch information...
1 parent 926687f commit f304b3180e98ea40616a4bc3edbc36cb9a2ec5ef @kierangraham kierangraham committed Jul 24, 2012
@@ -50,4 +50,7 @@ $(function(){
if($('.advanced-options input[value!=""], .advanced-options select option[value!=""]:selected').length > 2){
$('.advanced-options').show();
}
+
+ window.next_id = $("[data-next_id]").data("next_id");
+ window.next_key = $("[data-next_key]").data("next_key");
});
@@ -8,13 +8,26 @@ class ArticlesController < ApplicationController
def popular
@total = Article.view_stats[:count]
@per_page = 10
- @page = (params[:page] || 1).to_i
- @skip = (@page - 1) * @per_page
- @items = Article.popular(:limit => @per_page, :skip => @skip, :include_docs => true).entries
- # @items = WillPaginate::Collection.create(@page, @per_page, @total) do |pager|
- # pager.replace(@items.to_a)
- # end
+ # @page = (params[:page] || 1).to_i
+ # @skip = (@page - 1) * @per_page
+
+ options = { :limit => @per_page + 1, :include_docs => true }
+
+ # get documents from particular key and id
+ if params[:after_key].present? && params[:after_id].present?
+ options.merge!(:start_key => params[:after_key].to_i, :startkey_docid => params[:after_id])
+ end
+
+ @items = Article.popular(options).entries
+
+ # chop off the n+1th to form the next/previous links
+ @next_article = @items.slice! -1
+
+ if @next_article.present?
+ @next_id = @next_article.id
+ @next_key = @next_article.popularity
+ end
respond_to do |format|
format.html { render }
@@ -33,15 +46,27 @@ def index
end
# @items = Article.popular_by_type(type).take(10)
- @total = Article.view_stats[:count]
+ @total = Article.totals[type.to_sym]
@per_page = 10
- @page = (params[:page] || 1).to_i
- @skip = (@page - 1) * @per_page
- @items = Article.popular_by_type(:limit => @per_page, :skip => @skip, :type => type).entries
- # @items = WillPaginate::Collection.create(@page, @per_page, @total) do |pager|
- # pager.replace(@items.to_a)
- # end
+ options = { :limit => @per_page + 1, :include_docs => true, :type => type }
+
+ # get documents from particular key and id
+ if params[:after_key].present? && params[:after_id].present?
+ options.merge!(:startkey => [type, params[:after_key].to_i], :startkey_docid => params[:after_id])
+ end
+
+ @options = options
+
+ @items = Article.popular_by_type(options).entries
+
+ # chop off the n+1th to form the next/previous links
+ @next_article = @items.slice! -1
+
+ if @next_article.present?
+ @next_id = @next_article.id
+ @next_key = @next_article.popularity
+ end
respond_to do |format|
format.html { render }
View
@@ -252,7 +252,7 @@ def self.popular_by_type(opts={})
# Couch.client.design_docs["article"].by_type(:reduce => false).entries.collect { |row| Article.find(row.key[1]) }
type = opts.delete(:type)
options = { :reduce => false, :descending => true, :include_docs => true, :stale => false }.merge(opts)
- if type
+ if type && !(opts.include?(:startkey) || opts.include?(:start_key))
options.merge!({ :startkey => [type, Article.view_stats[:max]], :endkey => [type, 0] })
end
by_popularity_and_type(options).entries
@@ -1,4 +1,4 @@
-.page
+.page{ :data => { :next_id => @next_id, :next_key => @next_key } }
- @items.each do |item|
%article{:class => item['type']}
.article-body.clearfix
@@ -1 +1,5 @@
+<%- if @next_key.present? && @next_id.present? -%>
+ window.next_key = "<%= @next_key %>";
+ window.next_id = "<%= @next_id %>";
+<%- end -%>
$("<div class='page'><%= j(render :partial => 'shared/articles') %></div>").insertAfter(".page:last");
@@ -15,8 +15,17 @@
if (nearBottomOfPage()) {
loading=true;
page++;
- var location = window.location.href + ((window.location.href.indexOf("?") != -1) ? "&" : "?")
- $.getScript(location + "page=" + page)
+ var location = window.location.href;
+
+ if (window.next_key != undefined && window.next_id != undefined){
+ location += "?after_key=" + next_key;
+ location += "&after_id=" + next_id;
+ }
+ else {
+ location += "&page=" + page;
+ }
+
+ $.getScript(location)
.success(function() { loading = false })
.fail(function() { loading = true; $(".loading").hide(); })
}

0 comments on commit f304b31

Please sign in to comment.