FIX: Only use cached topic lists on popState

This commit is contained in:
Robin Ward 2014-09-09 12:57:13 -04:00
parent a96ba8ed78
commit 8b33e659a1
5 changed files with 34 additions and 25 deletions

View File

@ -24,10 +24,6 @@ export default Ember.Component.extend({
e.preventDefault();
// When you click the logo, never use a cached list
var session = Discourse.Session.current();
session.setProperties({topicList: null, topicListScrollPosition: null});
Discourse.URL.routeTo('/');
return false;
}

View File

@ -317,6 +317,7 @@ Discourse.URL = Em.Object.createWithMixins({
}
var transition = router.handleURL(path);
transition._discourse_intercepted = true;
transition.promise.then(function() {
if (elementId) {

View File

@ -193,34 +193,40 @@ Discourse.TopicList.reopenClass({
@method list
@param {Object} filter The menu item to filter to
@param {Object} params Any additional params to pass to TopicList.find()
@param {Object} extras Additional finding options, such as caching
@returns {Promise} a promise that resolves to the list of topics
**/
list: function(filter, params) {
var session = Discourse.Session.current(),
list = session.get('topicList'),
tracking = Discourse.TopicTrackingState.current();
list: function(filter, filterParams, extras) {
var tracking = Discourse.TopicTrackingState.current();
extras = extras || {};
return new Ember.RSVP.Promise(function(resolve) {
var session = Discourse.Session.current();
if (extras.cached) {
var cachedList = session.get('topicList');
// Try to use the cached version
if (list && (list.get('filter') === filter) &&
_.isEqual(list.get('listParams'), params)) {
list.set('loaded', true);
if (cachedList && (cachedList.get('filter') === filter) &&
_.isEqual(cachedList.get('listParams'), filterParams)) {
cachedList.set('loaded', true);
if (tracking) {
tracking.updateTopics(list.get('topics'));
tracking.updateTopics(cachedList.get('topics'));
}
return resolve(cachedList);
}
return resolve(list);
}
// Perform the search
// Clear the cache
session.setProperties({topicList: null, topicListScrollPosition: null});
// Clean up any string parameters that might slip through
params = params || {};
Ember.keys(params).forEach(function(k) {
var val = params[k];
filterParams = filterParams || {};
Ember.keys(filterParams).forEach(function(k) {
var val = filterParams[k];
if (val === "undefined" || val === "null" || val === 'false') {
params[k] = undefined;
filterParams[k] = undefined;
}
});
@ -233,10 +239,10 @@ Discourse.TopicList.reopenClass({
}
}
});
return resolve(Discourse.TopicList.find(filter, _.extend(findParams, params || {})));
return resolve(Discourse.TopicList.find(filter, _.extend(findParams, filterParams || {})));
}).then(function(list) {
list.set('listParams', params);
list.set('listParams', filterParams);
if (tracking) {
tracking.sync(list, list.filter);
tracking.trackIncoming(list.filter);

View File

@ -21,13 +21,15 @@ export default function(filter, extras) {
this.controllerFor('navigation/default').set('filterMode', filter);
},
model: function(data, transaction) {
model: function(data, transition) {
// attempt to stop early cause we need this to be called before .sync
Discourse.ScreenTrack.current().stop();
var findOpts = filterQueryParams(transaction.queryParams);
return Discourse.TopicList.list(filter, findOpts);
var findOpts = filterQueryParams(transition.queryParams),
extras = { cached: this.isPoppedState(transition) };
return Discourse.TopicList.list(filter, findOpts, extras);
},
setupController: function(controller, model, trans) {

View File

@ -41,6 +41,10 @@ Discourse.Route = Em.Route.extend({
});
}
}
},
isPoppedState: function(transition) {
return (!transition._discourse_intercepted) && (!!transition.intent.url);
}
});