我有一个简单的应用程序,您可以在其中登录,如下所示:
{{#view App.mainV}}
{{#if logged}}
Hey!
{{else}}
<!-- Login form here when clicked: App.mainC.login()-->
{{/if}}
{{/view}}
这是示例 Controller :
App.mainC = Ember.ArrayController.create({
login: function(){
if(ok)//Everything went fine
App.mainV.logged = true;
}
});
这是 mainV:
App.mainV = Ember.View.extend({
logged: false,
test: function()
{
console.log('JO');
}
});
我有两个关于此应用程序的问题:
- 为什么当我将记录更改为真实 View 时, View 没有改变?
如果我调用 App.mainV.test() 我会收到错误。为什么?
类型错误:“App.mainV.test”不是函数
最佳答案
如果您想从模板访问 View 的属性,则必须添加“view”前缀。在您的示例中:{{#if view.logged}}
。
您不能执行App.mainV.test()
,因为App.mainV
是一个类,而不是一个实例。您可以执行App.mainV.create().test()
。
您应该将 App.mainV
重命名为 App.MainV
以符合 Ember.js 约定(类名应大写,请参阅 http://www.emberist.com/2012/04/09/naming-conventions.html )
编辑:
您的示例中还有另一个问题: Controller 尝试修改 View 中的值。在 Ember 方式中,您应该将 View 的属性绑定(bind)到 Controller 。 Controller 中的任何更改都会传播到 View :
<script type="text/x-handlebars">
{{#view App.MainView}}
{{#if view.logged}}
Hey!
{{else}}
<!-- Login form here when clicked: App.mainController.login()-->
{{/if}}
{{/view}}
</script>
App = Ember.Application.create();
App.mainController = Ember.Controller.create({
logged: false,
login: function() {
this.set('logged', true);
}
});
App.MainView = Ember.View.extend({
loggedBinding: 'App.mainController.logged'
});
// change in controller will be propagated to the view
App.mainController.set('logged', true);
关于ember.js - 查看绑定(bind)Emberjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341622/