2013-07-12 54 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'。 –