2013-05-08 61 views
0

彙總計算屬性不燼彙總計算屬性不燼得到更新

Val.set('arr',[]); 
Val.set('arr',[{val:1},],[{val:2}],[{val:3}],[{val:4}]); 
Val.reopen({ 
    total:function(){ 
      var array=this.get('arr'); 
      var total=0; 
      for(i=0;i<array.length;i++) 
      { 
       total=total+array[i].val 
      } 
      return total; 
    }.property('[email protected]'), 
}); 

這個事情的作品是第一次,但物業觀察家從來沒有得到所謂每當數組元素更新第二次得到更新。

回答

0

如果你想觀察從數組中的對象的屬性,然後你缺少的東西在這裏:

... 
    }.property('[email protected]') 

嘗試更改您的代碼此

... 
    }.property('[email protected]') 

否則燼只會請注意數組本身的更改(刪除/添加元素),而不更改數組內部的這些對象的屬性。

沒有答案的一部分,但作爲一個靈感,你可以做你的總的方法更方便,像這樣,使用內置reduce方法:

Val.reopen({ 
    total:function(){ 
    var total = this.get('arr').reduce(function(a, b) { 
     return a.val + b.val; 
    }); 
    return total; 
    } 
}); 

希望它有助於