From 7ec34b205ac3ee98dece108033fee339667fb4be Mon Sep 17 00:00:00 2001 From: Gosha Arinich Date: Sat, 7 Sep 2013 18:52:20 +0300 Subject: [PATCH] inject currentUser into controllers & routes Through Ember's DI, instead of doing so via a mixin. --- app/assets/javascripts/discourse.js | 18 +++++++++++ .../controllers/composer_controller.js | 2 +- .../discourse/controllers/controller.js | 2 +- .../controllers/header_controller.js | 4 +-- .../discourse/controllers/list_controller.js | 8 ++--- .../controllers/object_controller.js | 4 +-- .../controllers/quote_button_controller.js | 2 +- .../discourse/controllers/topic_controller.js | 10 +++--- .../discourse/mixins/has_current_user.js | 31 ------------------- .../discourse/routes/user_routes.js | 4 +-- 10 files changed, 35 insertions(+), 50 deletions(-) delete mode 100644 app/assets/javascripts/discourse/mixins/has_current_user.js diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js index 728f0ff5557..ebfadc83d3c 100644 --- a/app/assets/javascripts/discourse.js +++ b/app/assets/javascripts/discourse.js @@ -230,3 +230,21 @@ Discourse = Ember.Application.createWithMixins(Discourse.Ajax, { Discourse.Router = Discourse.Router.reopen({ location: 'discourse_location' }); +Discourse.initializer({ + name: 'currentUser', + + initialize: function(container) { + container.register('user:current', Discourse.User.current(), { instantiate: false }); + } +}); + +Discourse.initializer({ + name: 'injectCurrentUser', + + initialize: function(container) { + if (container.lookup('user:current')) { + container.injection('controller', 'currentUser', 'user:current'); + container.injection('route', 'currentUser', 'user:current'); + } + } +}); diff --git a/app/assets/javascripts/discourse/controllers/composer_controller.js b/app/assets/javascripts/discourse/controllers/composer_controller.js index a5efa0ea8e4..7b59ccc04d8 100644 --- a/app/assets/javascripts/discourse/controllers/composer_controller.js +++ b/app/assets/javascripts/discourse/controllers/composer_controller.js @@ -98,7 +98,7 @@ Discourse.ComposerController = Discourse.Controller.extend({ opts = opts || {}; composerController.close(); - var currentUser = Discourse.User.current(); + var currentUser = this.get('currentUser'); if (composer.get('creatingTopic')) { currentUser.set('topic_count', currentUser.get('topic_count') + 1); } else { diff --git a/app/assets/javascripts/discourse/controllers/controller.js b/app/assets/javascripts/discourse/controllers/controller.js index ce298bced04..2c73a1a6dbe 100644 --- a/app/assets/javascripts/discourse/controllers/controller.js +++ b/app/assets/javascripts/discourse/controllers/controller.js @@ -7,4 +7,4 @@ @uses Discourse.Presence @module Discourse **/ -Discourse.Controller = Ember.Controller.extend(Discourse.Presence, Discourse.HasCurrentUser); +Discourse.Controller = Ember.Controller.extend(Discourse.Presence); diff --git a/app/assets/javascripts/discourse/controllers/header_controller.js b/app/assets/javascripts/discourse/controllers/header_controller.js index 790a299ad83..613c1586dfc 100644 --- a/app/assets/javascripts/discourse/controllers/header_controller.js +++ b/app/assets/javascripts/discourse/controllers/header_controller.js @@ -21,8 +21,8 @@ Discourse.HeaderController = Discourse.Controller.extend({ }.property(), showFavoriteButton: function() { - return Discourse.User.current() && !this.get('topic.isPrivateMessage'); - }.property('topic.isPrivateMessage'), + return this.get('currentUser') && !this.get('topic.isPrivateMessage'); + }.property('currentUser', 'topic.isPrivateMessage'), mobileDevice: function() { return Discourse.Session.currentProp('mobileDevice'); diff --git a/app/assets/javascripts/discourse/controllers/list_controller.js b/app/assets/javascripts/discourse/controllers/list_controller.js index 0fb4fd9292e..f1bf12c90d8 100644 --- a/app/assets/javascripts/discourse/controllers/list_controller.js +++ b/app/assets/javascripts/discourse/controllers/list_controller.js @@ -13,7 +13,7 @@ Discourse.ListController = Discourse.Controller.extend({ needs: ['composer', 'modal', 'listTopics'], availableNavItems: function() { - var loggedOn = !!Discourse.User.current(); + var loggedOn = !!this.get('currentUser'); return Discourse.SiteSettings.top_menu.split("|").map(function(i) { return Discourse.NavItem.fromText(i, { @@ -22,7 +22,7 @@ Discourse.ListController = Discourse.Controller.extend({ }).filter(function(i) { return i !== null; }); - }.property(), + }.property('currentUser'), createTopicText: function() { if (this.get('category.name')) { @@ -125,12 +125,12 @@ Discourse.ListController = Discourse.Controller.extend({ canEditCategory: function() { if( this.present('category') ) { - var u = Discourse.User.current(); + var u = this.get('currentUser'); return u && u.staff; } else { return false; } - }.property('category') + }.property('currentUser', 'category') }); diff --git a/app/assets/javascripts/discourse/controllers/object_controller.js b/app/assets/javascripts/discourse/controllers/object_controller.js index 966c9c52fde..5d8136703f3 100644 --- a/app/assets/javascripts/discourse/controllers/object_controller.js +++ b/app/assets/javascripts/discourse/controllers/object_controller.js @@ -7,6 +7,4 @@ @uses Discourse.Presence @module Discourse **/ -Discourse.ObjectController = Ember.ObjectController.extend(Discourse.Presence, Discourse.HasCurrentUser); - - +Discourse.ObjectController = Ember.ObjectController.extend(Discourse.Presence); diff --git a/app/assets/javascripts/discourse/controllers/quote_button_controller.js b/app/assets/javascripts/discourse/controllers/quote_button_controller.js index e0713bb8945..e8bb225fe88 100644 --- a/app/assets/javascripts/discourse/controllers/quote_button_controller.js +++ b/app/assets/javascripts/discourse/controllers/quote_button_controller.js @@ -31,7 +31,7 @@ Discourse.QuoteButtonController = Discourse.Controller.extend({ **/ selectText: function(postId) { // anonymous users cannot "quote-reply" - if (!Discourse.User.current()) return; + if (!this.get('currentUser')) return; // don't display the "quote-reply" button if we can't create a post if (!this.get('controllers.topic.model.details.can_create_post')) return; diff --git a/app/assets/javascripts/discourse/controllers/topic_controller.js b/app/assets/javascripts/discourse/controllers/topic_controller.js index 5f8932bf185..8ff7c724caf 100644 --- a/app/assets/javascripts/discourse/controllers/topic_controller.js +++ b/app/assets/javascripts/discourse/controllers/topic_controller.js @@ -260,8 +260,8 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected }, showFavoriteButton: function() { - return Discourse.User.current() && !this.get('isPrivateMessage'); - }.property('isPrivateMessage'), + return this.get('currentUser') && !this.get('isPrivateMessage'); + }.property('currentUser', 'isPrivateMessage'), recoverTopic: function() { this.get('content').recover(); @@ -269,7 +269,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected deleteTopic: function() { this.unsubscribe(); - this.get('content').destroy(Discourse.User.current()); + this.get('content').destroy(this.get('currentUser')); }, resetRead: function() { @@ -435,7 +435,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected }, toggleBookmark: function(post) { - if (!Discourse.User.current()) { + if (!this.get('currentUser')) { alert(I18n.t("bookmarks.not_bookmarked")); return; } @@ -457,7 +457,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected }, deletePost: function(post) { - var user = Discourse.User.current(), + var user = this.get('currentUser'), replyCount = post.get('reply_count'), self = this; diff --git a/app/assets/javascripts/discourse/mixins/has_current_user.js b/app/assets/javascripts/discourse/mixins/has_current_user.js deleted file mode 100644 index b23d3a28761..00000000000 --- a/app/assets/javascripts/discourse/mixins/has_current_user.js +++ /dev/null @@ -1,31 +0,0 @@ -/** - This mixin provides a `currentUser` property that can be used to retrieve information - about the currently logged in user. It is mostly useful to controllers so it can be - exposted to templates. - - Outside of templates, code should probably use `Discourse.User.current()` instead of - this property. - - @class Discourse.HasCurrentUser - @extends Ember.Mixin - @namespace Discourse - @module HasCurrentUser -**/ -Discourse.HasCurrentUser = Em.Mixin.create({ - - /** - Returns a reference to the currently logged in user. - - @method currentUser - @return {Discourse.User} the currently logged in user if present. - */ - currentUser: function() { - return Discourse.User.current(); - }.property().volatile() - -}); - - - - - diff --git a/app/assets/javascripts/discourse/routes/user_routes.js b/app/assets/javascripts/discourse/routes/user_routes.js index ab8d8699bf4..d5dd07f7541 100644 --- a/app/assets/javascripts/discourse/routes/user_routes.js +++ b/app/assets/javascripts/discourse/routes/user_routes.js @@ -12,7 +12,7 @@ Discourse.UserRoute = Discourse.Route.extend({ // If we're viewing the currently logged in user, return that object // instead. - var currentUser = Discourse.User.current(); + var currentUser = this.get('currentUser'); if (currentUser && (params.username.toLowerCase() === currentUser.get('username_lower'))) { return currentUser; } @@ -95,7 +95,7 @@ Discourse.UserActivityRoute = Discourse.Route.extend({ var composerController = this.controllerFor('composer'); controller.set('model', user); - if (Discourse.User.current()) { + if (this.get('currentUser')) { Discourse.Draft.get('new_private_message').then(function(data) { if (data.draft) { composerController.open({