2013-07-12 34 views
1

的负载等控制器我下面的代码有:Ember.js:计算财产,涉及未刷新在模型

App.UserController = App.EditableController.extend({ 
    needs: 'application', 
    test: function() { 
     return this.get('controller.application.me.email'); 
    }.property('controller.application.me.email'), 
    }); 

    App.ApplicationController = Ember.Controller.extend({ 
    isPublic : true, 
    me   : null, 
    init: function() { 
     this.set('me', App.User.find(1)); 
     this._super(); 
    } 
    }); 

但是计算的特性似乎并不一旦模型加载更新(从控制台) :

> App.__container__.lookup('controller:Application').get('me.email') 
"[email protected]" 
> App.__container__.lookup('controller:User').get('test') 
undefined 

我错过了什么吗?

回答

2

假设你App.EditableControllerEmber.ObjectController型,则这应该工作:

App.UserController = Ember.ObjectController.extend({ 
    needs: 'application', 
    // notice the we use here plural 'controllers' to have access to the 
    // controllers defined with the 'needs' API 
    contentBinding: 'controllers.application', 
    test: function() { 
    return this.get('content.me.email'); 
    }.property('content') 
}); 

在您App.EditableControllerEmber.Controller型的比这应该做的工作的情况下:

App.UserController = Ember.Controller.extend({ 
    needs: 'application', 
    // notice the we use here plural 'controllers' to have access to the 
    // controllers defined with the 'needs' API 
    controllerBinding: 'controllers.application', 
    test: function() { 
    return this.get('controller.me.email'); 
    }.property('controller') 
}); 

现在做App.__container__.lookup('controller:User').get('test')在控制台应输出:

"[email protected]" // or whatever your example mail is 

希望它有帮助。

+0

这样做的伎俩,非常感谢你! 'EditableController'确实是ObjectController的扩展。为什么'contentBinding'工作,但不是'controllerBinding'? –

+0

@BrianGates我很高兴我能帮上忙。至于contentBinding这是因为ObjectController需要定义content属性,因此我们在这里使用它,简单的'Controller'没有这样的属性,所以在这种情况下它也可以是'fooBinding' ,然后'property('foo')' – intuitivepixel

+0

进一步测试后,使用'contentBinding'似乎会破坏现有的'content'属性(这是有道理的),但是如果我使用任何其他值,我会收到一个错误'Assertion失败:无法将set('foo',)委托给对象代理的'content'属性:其'content'未定义。'做一点研究,我发现这:http://stackoverflow.com/questions/12502465/bindings-on-objectcontroller-ember-js。所以我所要做的就是声明一个'controller'属性,然后我可以使用'controllerBinding'。 –