diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js index 7fb9937720b..f71d9d104f6 100644 --- a/app/assets/javascripts/discourse.js +++ b/app/assets/javascripts/discourse.js @@ -229,21 +229,3 @@ 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 c289c57bb8d..7732468f5e3 100644 --- a/app/assets/javascripts/discourse/controllers/composer_controller.js +++ b/app/assets/javascripts/discourse/controllers/composer_controller.js @@ -103,7 +103,7 @@ Discourse.ComposerController = Discourse.Controller.extend({ opts = opts || {}; composerController.close(); - var currentUser = this.get('currentUser'); + var currentUser = Discourse.User.current(); 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 2c73a1a6dbe..ce298bced04 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.Controller = Ember.Controller.extend(Discourse.Presence, Discourse.HasCurrentUser); diff --git a/app/assets/javascripts/discourse/controllers/header_controller.js b/app/assets/javascripts/discourse/controllers/header_controller.js index bf52f41a984..4612ee7194c 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 this.get('currentUser') && !this.get('topic.isPrivateMessage'); - }.property('currentUser', 'topic.isPrivateMessage'), + return Discourse.User.current() && !this.get('topic.isPrivateMessage'); + }.property('topic.isPrivateMessage'), mobileDevice: function() { return Discourse.Mobile.isMobileDevice; diff --git a/app/assets/javascripts/discourse/controllers/list_controller.js b/app/assets/javascripts/discourse/controllers/list_controller.js index f1bf12c90d8..0fb4fd9292e 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 = !!this.get('currentUser'); + var loggedOn = !!Discourse.User.current(); 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('currentUser'), + }.property(), createTopicText: function() { if (this.get('category.name')) { @@ -125,12 +125,12 @@ Discourse.ListController = Discourse.Controller.extend({ canEditCategory: function() { if( this.present('category') ) { - var u = this.get('currentUser'); + var u = Discourse.User.current(); return u && u.staff; } else { return false; } - }.property('currentUser', 'category') + }.property('category') }); diff --git a/app/assets/javascripts/discourse/controllers/object_controller.js b/app/assets/javascripts/discourse/controllers/object_controller.js index 5d8136703f3..966c9c52fde 100644 --- a/app/assets/javascripts/discourse/controllers/object_controller.js +++ b/app/assets/javascripts/discourse/controllers/object_controller.js @@ -7,4 +7,6 @@ @uses Discourse.Presence @module Discourse **/ -Discourse.ObjectController = Ember.ObjectController.extend(Discourse.Presence); +Discourse.ObjectController = Ember.ObjectController.extend(Discourse.Presence, Discourse.HasCurrentUser); + + diff --git a/app/assets/javascripts/discourse/controllers/quote_button_controller.js b/app/assets/javascripts/discourse/controllers/quote_button_controller.js index e8bb225fe88..e0713bb8945 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 (!this.get('currentUser')) return; + if (!Discourse.User.current()) 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 27cc245c530..e6a01bbe405 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 this.get('currentUser') && !this.get('isPrivateMessage'); - }.property('currentUser', 'isPrivateMessage'), + return Discourse.User.current() && !this.get('isPrivateMessage'); + }.property('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(this.get('currentUser')); + this.get('content').destroy(Discourse.User.current()); }, resetRead: function() { @@ -417,7 +417,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected }, toggleBookmark: function(post) { - if (!this.get('currentUser')) { + if (!Discourse.User.current()) { alert(I18n.t("bookmarks.not_bookmarked")); return; } @@ -439,7 +439,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected }, deletePost: function(post) { - var user = this.get('currentUser'), + var user = Discourse.User.current(), 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 new file mode 100644 index 00000000000..b23d3a28761 --- /dev/null +++ b/app/assets/javascripts/discourse/mixins/has_current_user.js @@ -0,0 +1,31 @@ +/** + 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 d5dd07f7541..ab8d8699bf4 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 = this.get('currentUser'); + var currentUser = Discourse.User.current(); 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 (this.get('currentUser')) { + if (Discourse.User.current()) { Discourse.Draft.get('new_private_message').then(function(data) { if (data.draft) { composerController.open({