2016-04-20 21 views
0

所以我這裏面計算的屬性我component.jscontexts: Ember.computed.oneWay('myService.contexts'),Ember.js:從服務使用在車把上的計算性能

,我能夠從另一個動作得到上述內容

openHelp(){ 
     console.log(this.get('contexts')) 
     alert(this.get('contexts')) 
    } 
    } 

但當我嘗試使用Handlebars中的計算屬性({{contexts}})時,它只是空白。

我創建灰燼玩弄此問題:https://ember-twiddle.com/38de64d58dcf3298df6d4176f15cbc0e?openFiles=components.my-component-help.js%2Ctemplates.components.my-component-help.hbs

如果我有一個數組foo: [ 'foo','bar']和我{{FOO}}它車把輸出。但是,如果我使foo的計算屬性獲得[ 'foo','bar'],並且做了{{foo}},我什麼也得不到。

回答

2

這裏的解決方案:https://ember-twiddle.com/e9c2ef05e27013a389e0b2bfdaec3d40?openFiles=services.my-service.js%2Ctemplates.components.my-component-help.hbs

有兩個問題:

  1. contexts是一個數組。當你console.logalert它,這些方法在內部在一些瀏覽器JSON.stringify爲你方便的對象。 Ember不會那樣做。您需要自己格式化陣列,或者像我一樣,使用each。爲了進行調試,請隨時使用幫助器log

  2. 陣列上的計算屬性通過Ember的方法觀察陣列突變,如pushObjectremoveObject。只需使用pushsplice將不會更新計算的屬性。