inject currentUser into controllers & routes
Through Ember's DI, instead of doing so via a mixin.
This commit is contained in:
parent
3fb0f52574
commit
7ec34b205a
|
@ -230,3 +230,21 @@ Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
|
||||||
|
|
||||||
Discourse.Router = Discourse.Router.reopen({ location: 'discourse_location' });
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
@ -98,7 +98,7 @@ Discourse.ComposerController = Discourse.Controller.extend({
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
composerController.close();
|
composerController.close();
|
||||||
|
|
||||||
var currentUser = Discourse.User.current();
|
var currentUser = this.get('currentUser');
|
||||||
if (composer.get('creatingTopic')) {
|
if (composer.get('creatingTopic')) {
|
||||||
currentUser.set('topic_count', currentUser.get('topic_count') + 1);
|
currentUser.set('topic_count', currentUser.get('topic_count') + 1);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -7,4 +7,4 @@
|
||||||
@uses Discourse.Presence
|
@uses Discourse.Presence
|
||||||
@module Discourse
|
@module Discourse
|
||||||
**/
|
**/
|
||||||
Discourse.Controller = Ember.Controller.extend(Discourse.Presence, Discourse.HasCurrentUser);
|
Discourse.Controller = Ember.Controller.extend(Discourse.Presence);
|
||||||
|
|
|
@ -21,8 +21,8 @@ Discourse.HeaderController = Discourse.Controller.extend({
|
||||||
}.property(),
|
}.property(),
|
||||||
|
|
||||||
showFavoriteButton: function() {
|
showFavoriteButton: function() {
|
||||||
return Discourse.User.current() && !this.get('topic.isPrivateMessage');
|
return this.get('currentUser') && !this.get('topic.isPrivateMessage');
|
||||||
}.property('topic.isPrivateMessage'),
|
}.property('currentUser', 'topic.isPrivateMessage'),
|
||||||
|
|
||||||
mobileDevice: function() {
|
mobileDevice: function() {
|
||||||
return Discourse.Session.currentProp('mobileDevice');
|
return Discourse.Session.currentProp('mobileDevice');
|
||||||
|
|
|
@ -13,7 +13,7 @@ Discourse.ListController = Discourse.Controller.extend({
|
||||||
needs: ['composer', 'modal', 'listTopics'],
|
needs: ['composer', 'modal', 'listTopics'],
|
||||||
|
|
||||||
availableNavItems: function() {
|
availableNavItems: function() {
|
||||||
var loggedOn = !!Discourse.User.current();
|
var loggedOn = !!this.get('currentUser');
|
||||||
|
|
||||||
return Discourse.SiteSettings.top_menu.split("|").map(function(i) {
|
return Discourse.SiteSettings.top_menu.split("|").map(function(i) {
|
||||||
return Discourse.NavItem.fromText(i, {
|
return Discourse.NavItem.fromText(i, {
|
||||||
|
@ -22,7 +22,7 @@ Discourse.ListController = Discourse.Controller.extend({
|
||||||
}).filter(function(i) {
|
}).filter(function(i) {
|
||||||
return i !== null;
|
return i !== null;
|
||||||
});
|
});
|
||||||
}.property(),
|
}.property('currentUser'),
|
||||||
|
|
||||||
createTopicText: function() {
|
createTopicText: function() {
|
||||||
if (this.get('category.name')) {
|
if (this.get('category.name')) {
|
||||||
|
@ -125,12 +125,12 @@ Discourse.ListController = Discourse.Controller.extend({
|
||||||
|
|
||||||
canEditCategory: function() {
|
canEditCategory: function() {
|
||||||
if( this.present('category') ) {
|
if( this.present('category') ) {
|
||||||
var u = Discourse.User.current();
|
var u = this.get('currentUser');
|
||||||
return u && u.staff;
|
return u && u.staff;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}.property('category')
|
}.property('currentUser', 'category')
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,4 @@
|
||||||
@uses Discourse.Presence
|
@uses Discourse.Presence
|
||||||
@module Discourse
|
@module Discourse
|
||||||
**/
|
**/
|
||||||
Discourse.ObjectController = Ember.ObjectController.extend(Discourse.Presence, Discourse.HasCurrentUser);
|
Discourse.ObjectController = Ember.ObjectController.extend(Discourse.Presence);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ Discourse.QuoteButtonController = Discourse.Controller.extend({
|
||||||
**/
|
**/
|
||||||
selectText: function(postId) {
|
selectText: function(postId) {
|
||||||
// anonymous users cannot "quote-reply"
|
// 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
|
// 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;
|
if (!this.get('controllers.topic.model.details.can_create_post')) return;
|
||||||
|
|
|
@ -260,8 +260,8 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
|
||||||
},
|
},
|
||||||
|
|
||||||
showFavoriteButton: function() {
|
showFavoriteButton: function() {
|
||||||
return Discourse.User.current() && !this.get('isPrivateMessage');
|
return this.get('currentUser') && !this.get('isPrivateMessage');
|
||||||
}.property('isPrivateMessage'),
|
}.property('currentUser', 'isPrivateMessage'),
|
||||||
|
|
||||||
recoverTopic: function() {
|
recoverTopic: function() {
|
||||||
this.get('content').recover();
|
this.get('content').recover();
|
||||||
|
@ -269,7 +269,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
|
||||||
|
|
||||||
deleteTopic: function() {
|
deleteTopic: function() {
|
||||||
this.unsubscribe();
|
this.unsubscribe();
|
||||||
this.get('content').destroy(Discourse.User.current());
|
this.get('content').destroy(this.get('currentUser'));
|
||||||
},
|
},
|
||||||
|
|
||||||
resetRead: function() {
|
resetRead: function() {
|
||||||
|
@ -435,7 +435,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
|
||||||
},
|
},
|
||||||
|
|
||||||
toggleBookmark: function(post) {
|
toggleBookmark: function(post) {
|
||||||
if (!Discourse.User.current()) {
|
if (!this.get('currentUser')) {
|
||||||
alert(I18n.t("bookmarks.not_bookmarked"));
|
alert(I18n.t("bookmarks.not_bookmarked"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -457,7 +457,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
|
||||||
},
|
},
|
||||||
|
|
||||||
deletePost: function(post) {
|
deletePost: function(post) {
|
||||||
var user = Discourse.User.current(),
|
var user = this.get('currentUser'),
|
||||||
replyCount = post.get('reply_count'),
|
replyCount = post.get('reply_count'),
|
||||||
self = this;
|
self = this;
|
||||||
|
|
||||||
|
|
|
@ -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()
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ Discourse.UserRoute = Discourse.Route.extend({
|
||||||
|
|
||||||
// If we're viewing the currently logged in user, return that object
|
// If we're viewing the currently logged in user, return that object
|
||||||
// instead.
|
// instead.
|
||||||
var currentUser = Discourse.User.current();
|
var currentUser = this.get('currentUser');
|
||||||
if (currentUser && (params.username.toLowerCase() === currentUser.get('username_lower'))) {
|
if (currentUser && (params.username.toLowerCase() === currentUser.get('username_lower'))) {
|
||||||
return currentUser;
|
return currentUser;
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,7 @@ Discourse.UserActivityRoute = Discourse.Route.extend({
|
||||||
|
|
||||||
var composerController = this.controllerFor('composer');
|
var composerController = this.controllerFor('composer');
|
||||||
controller.set('model', user);
|
controller.set('model', user);
|
||||||
if (Discourse.User.current()) {
|
if (this.get('currentUser')) {
|
||||||
Discourse.Draft.get('new_private_message').then(function(data) {
|
Discourse.Draft.get('new_private_message').then(function(data) {
|
||||||
if (data.draft) {
|
if (data.draft) {
|
||||||
composerController.open({
|
composerController.open({
|
||||||
|
|
Loading…
Reference in New Issue