FIX: Pagination while sorting on column headers

This commit is contained in:
Robin Ward 2013-11-14 12:35:18 -05:00
parent 07cbf8f4e3
commit 41dfcd2774
3 changed files with 18 additions and 21 deletions

View File

@ -11,10 +11,6 @@ Discourse.UserTopicsListController = Discourse.ObjectController.extend({
actions: { actions: {
loadMore: function() { loadMore: function() {
this.get('model').loadMore(); this.get('model').loadMore();
},
changeSort: function() {
console.log('sort changed!');
} }
} }

View File

@ -71,7 +71,7 @@ Discourse.TopicList = Discourse.Model.extend({
topics.clear(); topics.clear();
topics.pushObjects(newTopics); topics.pushObjects(newTopics);
self.set('loaded', true); self.setProperties({ loaded: true, more_topics_url: result.topic_list.more_topics_url });
}); });
}.observes('sortOrder.order', 'sortOrder.descending'), }.observes('sortOrder.order', 'sortOrder.descending'),
@ -82,26 +82,24 @@ Discourse.TopicList = Discourse.Model.extend({
var moreUrl = this.get('more_topics_url'); var moreUrl = this.get('more_topics_url');
if (moreUrl) { if (moreUrl) {
var topicList = this; var self = this;
this.set('loadingMore', true); this.set('loadingMore', true);
return Discourse.ajax({url: moreUrl}).then(function (result) { return Discourse.ajax({url: moreUrl}).then(function (result) {
var topicsAdded = 0; var topicsAdded = 0;
if (result) { if (result) {
// the new topics loaded from the server // the new topics loaded from the server
var newTopics = Discourse.TopicList.topicsFrom(result); var newTopics = Discourse.TopicList.topicsFrom(result),
var topics = topicList.get("topics"); topics = self.get("topics");
topicList.forEachNew(newTopics, function(t) { self.forEachNew(newTopics, function(t) {
t.set('highlight', topicsAdded++ === 0); t.set('highlight', topicsAdded++ === 0);
topics.pushObject(t); topics.pushObject(t);
}); });
topicList.set('more_topics_url', result.topic_list.more_topics_url); self.setProperties({ loadingMore: false, more_topics_url: result.topic_list.more_topics_url });
Discourse.Session.currentProp('topicList', topicList); Discourse.Session.currentProp('topicList', self);
topicList.set('loadingMore', false); return self.get('more_topics_url');
return result.topic_list.more_topics_url;
} }
}); });
} else { } else {

View File

@ -58,9 +58,9 @@ class ListController < ApplicationController
@description = @category.description @description = @category.description
if params[:parent_category].present? if params[:parent_category].present?
list.more_topics_url = url_for(category_list_parent_path(params[:parent_category], params[:category], page: next_page, format: "json")) list.more_topics_url = url_for(category_list_parent_path(params[:parent_category], params[:category], next_page_params))
else else
list.more_topics_url = url_for(category_list_path(params[:category], page: next_page, format: "json")) list.more_topics_url = url_for(category_list_path(params[:category], next_page_params))
end end
respond(list) respond(list)
@ -108,8 +108,12 @@ class ListController < ApplicationController
end end
end end
def next_page def next_page_params(opts=nil)
params[:page].to_i + 1 opts = opts || {}
route_params = { format: 'json', page: params[:page].to_i + 1 }
route_params[:sort_order] = opts[:sort_order] if opts[:sort_order].present?
route_params[:sort_descending] = opts[:sort_descending] if opts[:sort_descending].present?
route_params
end end
private private
@ -155,12 +159,11 @@ class ListController < ApplicationController
end end
def generate_list_for(action, target_user, opts) def generate_list_for(action, target_user, opts)
list = TopicQuery.new(current_user, opts) TopicQuery.new(current_user, opts).send("list_#{action}", target_user)
list = list.send("list_#{action}", target_user)
end end
def construct_url_with(action, opts, url_prefix=nil) def construct_url_with(action, opts, url_prefix=nil)
method = url_prefix.blank? ? "#{action}_path" : "#{url_prefix}_#{action}_path" method = url_prefix.blank? ? "#{action}_path" : "#{url_prefix}_#{action}_path"
public_send(method, opts.merge(format: 'json', page: next_page)) public_send(method, opts.merge(next_page_params(opts)))
end end
end end