ObjectController上的绑定 - Ember.js

时间:2012-09-19 20:33:44

标签: binding ember.js

当您尝试向ObjectController添加绑定时,它不起作用。

App.FailController = Em.ObjectController.extend({
    content: null,
    myBinding: "App.router.myController" // <-- fails
});

错误:

Uncaught Error: assertion failed: Cannot delegate set('my', ) to the 'content' property of object proxy <.FailController:ember154>: its 'content' is undefined.

尝试将其添加到content属性。

jsFiddle:demo

2 个答案:

答案 0 :(得分:33)

信用卡caligo-mentis {g}更多answered来自。{/ p>

ObjectProxy委托setcontent属性的任何调用,除非ObjectProxy实例上存在具有相同名称的属性。简单的解决方案是在声明绑定之前定义具有所需名称的属性。

App.FailController = Em.ObjectController.extend({
    my: null,
    myBinding: "App.router.myController" // <-- works
});

jsFiddle:demo

答案 1 :(得分:4)

替代解决方案:

App.FailController = Em.ObjectController.extend({
    content: Ember.Object.create(),
    my: function() {
      return App.router.myController;
    }.property('App.router.myController')
});

或更好:

App.FailController = Em.ObjectController.extend({
    content: Ember.Object.create(),
    my: Ember.computed.alias('App.router.myController')
});