Ember.js 如何设置余烬控制器属性
我正在查看的Ember应用程序的应用程序模板使用条件检查来确定要显示哪些链接Ember.js 如何设置余烬控制器属性,ember.js,Ember.js,我正在查看的Ember应用程序的应用程序模板使用条件检查来确定要显示哪些链接 {{#if isAuthenticated}} link {{else}} link... {{/if}} isAuthenticated属性是根据用户是否已注册/登录而有条件设置的 App.AuthController = Ember.ObjectController.extend({ currentUser: null, isAuthenticated: Em.computed.n
{{#if isAuthenticated}}
link
{{else}}
link...
{{/if}}
isAuthenticated属性是根据用户是否已注册/登录而有条件设置的
App.AuthController = Ember.ObjectController.extend({
currentUser: null,
isAuthenticated: Em.computed.notEmpty("currentUser.email"),
login: function(route) {
var me;
me = this;
return $.ajax({
url: "/users/sign_in.json",
type: "POST",
data: {
"user[email]": route.currentModel.email,
"user[password]": route.currentModel.password
},
success: function(data) {
me.set('currentUser', data.user);
return route.transitionTo('user', data.user);
应用程序能够毫无问题地处理注册和登录,但是,应用程序模板始终显示用户未经身份验证时的链接。是否有某些原因导致isAuthenticated在登录时不会根据您在此处看到的代码进行更新?如果@mavilein和@chopper允许我发布一个明确的答案来全面回答这个问题,那么下面就是: 应用控制器 由于您的
ApplicationController
是备份应用程序模板的工具,因此您应该使用所需的API来请求AuthController
,如下所示:
App.ApplicationController = Ember.ObjectController.extend({
needs: ['auth']
...
});
应用程序模板
然后,您可以访问AuthController
的所有属性,并以controllers.auth
作为访问的前缀,因此在应用程序模板中,您可以执行以下操作:
{{#if controllers.auth.isAuthenticated}}
link
{{else}}
link...
{{/if}}
如果您不喜欢长名称,还有一个快捷方式:
App.ApplicationController = Ember.ObjectController.extend({
needs: ['auth'],
isAuthenticated: Ember.computed.alias('controllers.auth.isAuthenticated')
...
});
这样,您就可以在应用程序模板中简单地执行以下操作:
{{#if isAuthenticated}}
link
{{else}}
link...
{{/if}}
在这种情况下,它指的是ApplicationController
已验证的属性,而该属性又是从原始控制器AuthController
计算出来的
希望有帮助。您说您的车把片段来自应用程序
模板,但您的控制器名为App.AuthController
。因此,您的模板未连接到控制器。相反,应用程序模板要求ApplicationController实例进行身份验证。这是不存在的,因此是错误的,@mavilein谢谢,有没有办法从AuthController中设置ApplicationController的属性?在ApplicationController中,指定需要:[“auth”]
。然后您可以通过controllers.auth.isAuthenticated
访问它。您总是可以像往常一样发布一个很好的答案:-)