diff --git a/app/assets/javascripts/discourse/models/category.js.es6 b/app/assets/javascripts/discourse/models/category.js.es6 index 9c2576b1b..01e281c62 100644 --- a/app/assets/javascripts/discourse/models/category.js.es6 +++ b/app/assets/javascripts/discourse/models/category.js.es6 @@ -137,13 +137,10 @@ const Category = RestModel.extend({ }.property('topics'), unreadTopics: function() { - // TODO this is somehow null for /categories page anon - if (!this.topicTrackingState) { return 0; } return this.topicTrackingState.countUnread(this.get('id')); }.property('topicTrackingState.messageCount'), newTopics: function() { - if (!this.topicTrackingState) { return 0; } return this.topicTrackingState.countNew(this.get('id')); }.property('topicTrackingState.messageCount'), diff --git a/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 b/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 index f72d69881..d1d804614 100644 --- a/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 +++ b/app/assets/javascripts/discourse/models/topic-tracking-state.js.es6 @@ -336,32 +336,16 @@ const TopicTrackingState = Discourse.Model.extend({ }); } } + + }); - -TopicTrackingState.reopenClass({ - - createFromStates(data) { - // TODO: This should be a model that does injection automatically - const container = Discourse.__container__, - messageBus = container.lookup('message-bus:main'), - currentUser = container.lookup('current-user:main'), - instance = TopicTrackingState.create({ messageBus, currentUser }); - - instance.loadStates(data); - instance.initialStatesLength = data && data.length; - instance.establishChannels(); - return instance; - }, - - current() { - if (!this.tracker) { - const data = PreloadStore.get('topicTrackingStates'); - this.tracker = this.createFromStates(data); - PreloadStore.remove('topicTrackingStates'); - } - return this.tracker; - } -}); +export function startTracking(tracking) { + const data = PreloadStore.get('topicTrackingStates'); + tracking.loadStates(data); + tracking.initialStatesLength = data && data.length; + tracking.establishChannels(); + PreloadStore.remove('topicTrackingStates'); +} export default TopicTrackingState; diff --git a/app/assets/javascripts/discourse/pre-initializers/inject-discourse-objects.js.es6 b/app/assets/javascripts/discourse/pre-initializers/inject-discourse-objects.js.es6 index 1d47530d5..7d66de697 100644 --- a/app/assets/javascripts/discourse/pre-initializers/inject-discourse-objects.js.es6 +++ b/app/assets/javascripts/discourse/pre-initializers/inject-discourse-objects.js.es6 @@ -5,7 +5,7 @@ import Store from 'discourse/models/store'; import DiscourseURL from 'discourse/lib/url'; import DiscourseLocation from 'discourse/lib/discourse-location'; import SearchService from 'discourse/services/search'; -import TopicTrackingState from 'discourse/models/topic-tracking-state'; +import { startTracking, default as TopicTrackingState } from 'discourse/models/topic-tracking-state'; function inject() { const app = arguments[0], @@ -31,11 +31,15 @@ export default { app.register('store:main', Store); inject(app, 'store', 'route', 'controller'); - app.register('message-bus:main', window.MessageBus, { instantiate: false }); + const messageBus = window.MessageBus; + app.register('message-bus:main', messageBus, { instantiate: false }); injectAll(app, 'messageBus'); - app.register('current-user:main', Discourse.User.current(), { instantiate: false }); - app.register('topic-tracking-state:main', TopicTrackingState.current(), { instantiate: false }); + const currentUser = Discourse.User.current(); + app.register('current-user:main', currentUser, { instantiate: false }); + + const tracking = TopicTrackingState.create({ messageBus, currentUser }); + app.register('topic-tracking-state:main', tracking, { instantiate: false }); injectAll(app, 'topicTrackingState'); const site = Discourse.Site.current(); @@ -58,5 +62,7 @@ export default { const keyValueStore = new KeyValueStore("discourse_"); app.register('key-value-store:main', keyValueStore, { instantiate: false }); injectAll(app, 'keyValueStore'); + + startTracking(tracking); } }; diff --git a/test/javascripts/helpers/create-store.js.es6 b/test/javascripts/helpers/create-store.js.es6 index 9bfa00be4..3632dba23 100644 --- a/test/javascripts/helpers/create-store.js.es6 +++ b/test/javascripts/helpers/create-store.js.es6 @@ -18,7 +18,7 @@ export default function() { return (this._kvs); } if (type === "topic-tracking-state:main") { - this._tracker = this._tracker || TopicTrackingState.current(); + this._tracker = this._tracker || TopicTrackingState.create(); return (this._tracker); } if (type === "site-settings:main") {