2013-04-04 31 views
0

我正在嘗試使用計算屬性,該屬性觀察數組中每個元素的特定屬性的更改。這是小提琴。點擊更改按鈕,並且計算的屬性不會觸發。爲什麼?爲什麼不是這個爲@each計算的屬性射擊?

這裏是小提琴: http://jsfiddle.net/inconduit/PkT8x/145/

和這裏的相關代碼

App.color1 = Ember.Object.create({ color : "red"}); 

// a contrived object that holds an array of color objects 
App.colorsHolder = Ember.Object.create({ 
    colors : Ember.A([App.color1]), 
}); 

App.ApplicationController = Ember.Controller.extend({ 
    colorsHolder : App.colorsHolder, 

    // this should fire when you click the change button, but it does not 
    colorsContainBlue : function() { 
     console.log("fired colorsContainBlue"); 
     this.colorsHolder.colors.forEach(function(colorObj) { 
      if(colorObj.get('color') == 'blue') 
       return true; 
     }); 
     return false; 
    }.property('[email protected]'),         

    // this is a function called by an action in the template 
    changeToBlue: function() { 
     App.color1.set('color','blue'); 
     console.log("changed the color to: " + App.color1.get('color')); 
    } 
}); 

回答

2

這是小提琴根據您所提供的一個工作示例。

http://jsfiddle.net/skane/PkT8x/147/

colorsContainBlue : function() { 
    console.log("fired colorsContainBlue"); 
    if (this.colorsHolder.filterProperty('color', 'blue').length !== 0) { 
     return true; 
    } 
    return false; 
}.property('[email protected]'),         

changeToBlue: function() { 
    this.get('colorsHolder').objectAt(0).set('color','blue'); 
} 

我已經在你的例子包括改變很多東西:

  1. changeToBlue現在改變了ApplicationController中的colorHolder對象的屬性(重要)
  2. 計算屬性現在觀察colorsHolder對象和具體觀察他們的「顏色」託
  3. 我已經使用filterProp erty以確定是否有任何對象具有一個顏色值===「藍色」
相關問題