Move MessageBus subscribing/unsubscribing out of view

This commit is contained in:
Robin Ward 2013-06-07 14:28:33 -04:00
parent 5217602ec3
commit 668a4a3042
3 changed files with 13 additions and 9 deletions

View File

@ -86,7 +86,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
},
selectAll: function() {
var posts = this.get('posts');
var posts = this.get('posts');
var selectedPosts = this.get('selectedPosts');
if (posts) {
selectedPosts.addObjects(posts);
@ -321,14 +321,15 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
// Receive notifications for this topic
subscribe: function() {
// Unsubscribe before subscribing again
this.unsubscribe();
var bus = Discourse.MessageBus;
// there is a condition where the view never calls unsubscribe, navigate to a topic from a topic
bus.unsubscribe('/topic/*');
var topicController = this;
bus.subscribe("/topic/" + (this.get('content.id')), function(data) {
var topic = topicController.get('content');
bus.subscribe("/topic/" + (this.get('id')), function(data) {
var topic = topicController.get('model');
if (data.notification_level_change) {
topic.set('notification_level', data.notification_level_change);
topic.set('notifications_reason_id', data.notifications_reason_id);
@ -353,7 +354,9 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
unsubscribe: function() {
var topicId = this.get('content.id');
if (!topicId) return;
Discourse.MessageBus.unsubscribe("/topic/" + topicId);
// there is a condition where the view never calls unsubscribe, navigate to a topic from a topic
Discourse.MessageBus.unsubscribe('/topic/*');
},
// Post related methods

View File

@ -88,6 +88,8 @@ Discourse.TopicRoute = Discourse.Route.extend({
var headerController, topicController;
topicController = this.controllerFor('topic');
topicController.cancelFilter();
topicController.unsubscribe();
topicController.set('multiSelect', false);
this.controllerFor('composer').set('topic', null);
@ -101,6 +103,7 @@ Discourse.TopicRoute = Discourse.Route.extend({
controller.set('model', model);
this.controllerFor('header').set('topic', model);
this.controllerFor('composer').set('topic', model);
controller.subscribe();
}
});

View File

@ -100,7 +100,6 @@ Discourse.TopicView = Discourse.View.extend(Discourse.Scrolling, {
$(window).unbind('resize.discourse-on-scroll');
controller = this.get('controller');
controller.unsubscribe();
controller.set('onPostRendered', null);
screenTrack = this.get('screenTrack');
@ -121,7 +120,6 @@ Discourse.TopicView = Discourse.View.extend(Discourse.Scrolling, {
$(window).bind('resize.discourse-on-scroll', function() { topicView.updatePosition(false); });
var controller = this.get('controller');
controller.subscribe();
controller.set('onPostRendered', function(){
topicView.postsRendered.apply(topicView);
});