这是我的初始化程序:
...
Ember.SimpleAuth.Session.reopen({
currentUser: function() {
var userId = this.get('user_id');
if (!Ember.isEmpty(userId)) {
return container.lookup('store:main').find('user', userId);
}
}.property('user_id')
});
...
Controller :
isAdmin: function() {
var session = this.get('session.currentUser'),
role = session.get('role'); // 'role' is undefined
return role.get('name') == "Administrator";
}.property()
但是当我尝试使用模板时:
{{session.currentUser.role.name}}
它工作得很好。
如何访问所有 Controller 甚至路由中的currentUser
?
最佳答案
我认为这是因为 session.currentUser
是一个 promise 。在您的 Controller 中尝试一下:
isAdmin: function() {
return this.get('session.currentUser').then(function(r) {
return r.get('role.name') == 'Administrator';
});
}.property()
关于javascript - 如何从 Controller 访问 currentUser?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24179730/