diff --git a/app/assets/javascripts/discourse/routes/discourse_location.js b/app/assets/javascripts/discourse/routes/discourse_location.js index 43c86ae73..2f8b56da6 100644 --- a/app/assets/javascripts/discourse/routes/discourse_location.js +++ b/app/assets/javascripts/discourse/routes/discourse_location.js @@ -34,6 +34,7 @@ $.parseParams = function(query) { return params; }; +var popstateCallbacks = []; /** `Ember.DiscourseLocation` implements the location API using the browser's @@ -222,7 +223,9 @@ Ember.DiscourseLocation = Ember.Object.extend({ popstateFired = true; if (self.getURL() === self._previousURL) { return; } } - callback(self.getURL()); + var url = self.getURL(); + popstateCallbacks.forEach(function(cb) { cb(url); }); + callback(url); }); }, @@ -253,3 +256,25 @@ Ember.DiscourseLocation = Ember.Object.extend({ }); Ember.Location.registerImplementation('discourse_location', Ember.DiscourseLocation); + +/** + Since we're using pushState/replaceState let's add extra hooks to cloakedView to + eject itself when the popState occurs. This results in better back button + behavior. +**/ +Ember.CloakedCollectionView.reopen({ + _watchForPopState: function() { + var self = this, + cb = function() { + self.cleanUp(); + self.set('controller.postStream.loaded', false); + }; + this.set('_callback', cb); + popstateCallbacks.addObject(cb); + }.on('didInsertElement'), + + _disbandWatcher: function() { + popstateCallbacks.removeObject(this.get('_callback')); + this.set('_callback', null); + }.on('willDestroyElement') +});