inject currentUser into controllers & routes

Through Ember's DI, instead of doing so via a mixin.
This commit is contained in:
Gosha Arinich 2013-09-07 18:52:20 +03:00
parent 3fb0f52574
commit 7ec34b205a
10 changed files with 35 additions and 50 deletions

View file

@ -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');
}
}
});

View file

@ -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 {

View file

@ -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);

View file

@ -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');

View file

@ -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')
});

View file

@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -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()
});

View file

@ -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({