FIX: sorting was not working in /top

PERF: remove double request when sorting topics lists
This commit is contained in:
Régis Hanol 2015-10-12 21:58:40 +02:00
parent 207cb4ff0c
commit 1a3e9cf571
6 changed files with 33 additions and 51 deletions

View File

@ -16,8 +16,6 @@ var controllerOpts = {
}; };
// Aliases for the values // Aliases for the values
controllerOpts.queryParams.forEach(function(p) { controllerOpts.queryParams.forEach(p => controllerOpts[p] = Em.computed.alias(`controllers.discovery/topics.${p}`));
controllerOpts[p] = Em.computed.alias('controllers.discovery/topics.' + p);
});
export default Ember.Controller.extend(controllerOpts); export default Ember.Controller.extend(controllerOpts);

View File

@ -25,6 +25,7 @@ const controllerOpts = {
} else { } else {
this.setProperties({ order: sortBy, ascending: false }); this.setProperties({ order: sortBy, ascending: false });
} }
this.get('model').refreshSort(sortBy, this.get('ascending')); this.get('model').refreshSort(sortBy, this.get('ascending'));
}, },
@ -41,7 +42,7 @@ const controllerOpts = {
refresh() { refresh() {
const filter = this.get('model.filter'); const filter = this.get('model.filter');
this.setProperties({ order: 'default', ascending: false }); this.setProperties({ order: "default", ascending: false });
// Don't refresh if we're still loading // Don't refresh if we're still loading
if (this.get('controllers.discovery.loading')) { return; } if (this.get('controllers.discovery.loading')) { return; }
@ -51,7 +52,7 @@ const controllerOpts = {
// Lesson learned: Don't call `loading` yourself. // Lesson learned: Don't call `loading` yourself.
this.set('controllers.discovery.loading', true); this.set('controllers.discovery.loading', true);
this.store.findFiltered('topicList', {filter}).then((list) => { this.store.findFiltered('topicList', {filter}).then(list => {
Discourse.TopicList.hideUniformCategory(list, this.get('category')); Discourse.TopicList.hideUniformCategory(list, this.get('category'));
this.setProperties({ model: list }); this.setProperties({ model: list });

View File

@ -25,51 +25,33 @@ function topicsFrom(result, store) {
const TopicList = RestModel.extend({ const TopicList = RestModel.extend({
canLoadMore: Em.computed.notEmpty("more_topics_url"), canLoadMore: Em.computed.notEmpty("more_topics_url"),
forEachNew: function(topics, callback) { forEachNew(topics, callback) {
const topicIds = []; const topicIds = [];
_.each(this.get('topics'),function(topic) {
topicIds[topic.get('id')] = true;
});
_.each(topics,function(topic) { _.each(this.get('topics'), topic => topicIds[topic.get('id')] = true);
if(!topicIds[topic.id]) {
_.each(topics, topic => {
if (!topicIds[topic.id]) {
callback(topic); callback(topic);
} }
}); });
}, },
refreshSort: function(order, ascending) { refreshSort(order, ascending) {
const self = this; let params = this.get('params') || {};
var params = this.get('params') || {};
params.order = order || params.order;
if (ascending === undefined) {
params.ascending = ascending;
} else {
params.ascending = ascending;
}
if (params.q) { if (params.q) {
// search is unique, nothing else allowed with it // search is unique, nothing else allowed with it
params = {q: params.q}; params = { q: params.q };
} else {
params.order = order || params.order;
params.ascending = ascending;
} }
this.set('loaded', false);
this.set('params', params); this.set('params', params);
const store = this.store;
store.findFiltered('topicList', {filter: this.get('filter'), params}).then(function(tl) {
const newTopics = tl.get('topics'),
topics = self.get('topics');
topics.clear();
topics.pushObjects(newTopics);
self.setProperties({ loaded: true, more_topics_url: tl.get('topic_list.more_topics_url') });
});
}, },
loadMore: function() { loadMore() {
if (this.get('loadingMore')) { return Ember.RSVP.resolve(); } if (this.get('loadingMore')) { return Ember.RSVP.resolve(); }
const moreUrl = this.get('more_topics_url'); const moreUrl = this.get('more_topics_url');
@ -108,19 +90,17 @@ const TopicList = RestModel.extend({
// loads topics with these ids "before" the current topics // loads topics with these ids "before" the current topics
loadBefore: function(topic_ids){ loadBefore(topic_ids) {
const topicList = this, const topicList = this,
topics = this.get('topics'); topics = this.get('topics');
// refresh dupes // refresh dupes
topics.removeObjects(topics.filter(function(topic){ topics.removeObjects(topics.filter(topic => topic_ids.indexOf(topic.get('id')) >= 0));
return topic_ids.indexOf(topic.get('id')) >= 0;
}));
const url = Discourse.getURL("/") + this.get('filter') + "?topic_ids=" + topic_ids.join(",");
const url = `${Discourse.getURL("/")}${this.get('filter')}?topic_ids=${topic_ids.join(",")}`;
const store = this.store; const store = this.store;
return Discourse.ajax({ url }).then(function(result) {
return Discourse.ajax({ url }).then(result => {
let i = 0; let i = 0;
topicList.forEachNew(topicsFrom(result, store), function(t) { topicList.forEachNew(topicsFrom(result, store), function(t) {
// highlight the first of the new topics so we can get a visual feedback // highlight the first of the new topics so we can get a visual feedback

View File

@ -17,6 +17,10 @@ const User = RestModel.extend({
hasNotPosted: Em.computed.not("hasPosted"), hasNotPosted: Em.computed.not("hasPosted"),
canBeDeleted: Em.computed.and("can_be_deleted", "hasNotPosted"), canBeDeleted: Em.computed.and("can_be_deleted", "hasNotPosted"),
redirected_to_top: {
reason: null,
},
@computed() @computed()
stream() { stream() {
return UserStream.create({ user: this }); return UserStream.create({ user: this });

View File

@ -12,27 +12,27 @@ export default {
app.DiscoveryCategoryNoneRoute = buildCategoryRoute('latest', {no_subcategories: true}); app.DiscoveryCategoryNoneRoute = buildCategoryRoute('latest', {no_subcategories: true});
const site = Discourse.Site.current(); const site = Discourse.Site.current();
site.get('filters').forEach(function(filter) { site.get('filters').forEach(filter => {
app["Discovery" + filter.capitalize() + "Controller"] = DiscoverySortableController.extend(); app["Discovery" + filter.capitalize() + "Controller"] = DiscoverySortableController.extend();
app["Discovery" + filter.capitalize() + "Route"] = buildTopicRoute(filter); app["Discovery" + filter.capitalize() + "Route"] = buildTopicRoute(filter);
app["Discovery" + filter.capitalize() + "CategoryRoute"] = buildCategoryRoute(filter); app["Discovery" + filter.capitalize() + "CategoryRoute"] = buildCategoryRoute(filter);
app["Discovery" + filter.capitalize() + "CategoryNoneRoute"] = buildCategoryRoute(filter, {no_subcategories: true}); app["Discovery" + filter.capitalize() + "CategoryNoneRoute"] = buildCategoryRoute(filter, {no_subcategories: true});
}); });
Discourse.DiscoveryTopController = DiscoverySortableController.extend();
Discourse.DiscoveryTopRoute = buildTopicRoute('top', { Discourse.DiscoveryTopRoute = buildTopicRoute('top', {
actions: { actions: {
willTransition: function() { willTransition() {
this._super();
Discourse.User.currentProp("should_be_redirected_to_top", false); Discourse.User.currentProp("should_be_redirected_to_top", false);
Discourse.User.currentProp("redirected_to_top.reason", null); Discourse.User.currentProp("redirected_to_top.reason", null);
return true; return this._super();
} }
} }
}); });
Discourse.DiscoveryTopCategoryRoute = buildCategoryRoute('top'); Discourse.DiscoveryTopCategoryRoute = buildCategoryRoute('top');
Discourse.DiscoveryTopCategoryNoneRoute = buildCategoryRoute('top', {no_subcategories: true}); Discourse.DiscoveryTopCategoryNoneRoute = buildCategoryRoute('top', {no_subcategories: true});
site.get('periods').forEach(function(period) {
site.get('periods').forEach(period => {
app["DiscoveryTop" + period.capitalize() + "Controller"] = DiscoverySortableController.extend(); app["DiscoveryTop" + period.capitalize() + "Controller"] = DiscoverySortableController.extend();
app["DiscoveryTop" + period.capitalize() + "Route"] = buildTopicRoute('top/' + period); app["DiscoveryTop" + period.capitalize() + "Route"] = buildTopicRoute('top/' + period);
app["DiscoveryTop" + period.capitalize() + "CategoryRoute"] = buildCategoryRoute('top/' + period); app["DiscoveryTop" + period.capitalize() + "CategoryRoute"] = buildCategoryRoute('top/' + period);

View File

@ -15,7 +15,6 @@ function filterQueryParams(params, defaultParams) {
function findTopicList(store, tracking, filter, filterParams, extras) { function findTopicList(store, tracking, filter, filterParams, extras) {
extras = extras || {}; extras = extras || {};
return new Ember.RSVP.Promise(function(resolve) { return new Ember.RSVP.Promise(function(resolve) {
const session = Discourse.Session.current(); const session = Discourse.Session.current();
if (extras.cached) { if (extras.cached) {
@ -72,7 +71,7 @@ export default function(filter, extras) {
// attempt to stop early cause we need this to be called before .sync // attempt to stop early cause we need this to be called before .sync
ScreenTrack.current().stop(); ScreenTrack.current().stop();
const findOpts = filterQueryParams(transition.queryParams), const findOpts = filterQueryParams(data),
findExtras = { cached: this.isPoppedState(transition) }; findExtras = { cached: this.isPoppedState(transition) };
return findTopicList(this.store, this.topicTrackingState, filter, findOpts, findExtras); return findTopicList(this.store, this.topicTrackingState, filter, findOpts, findExtras);