2013-07-31 71 views
6
self.resultList.forEach(function(item, index, enumerable){ 
         console.log(self.resultList); 
         item.id=11; 
         item.get('id'); 
        }); 

這樣的項目: enter image description here如何使用Ember.js Array forEach更改值?

如果item.id = 11; 異常這樣的:

Assertion failed: You must use Ember.set() to access this property (of [object Object])

所以item.get( 'ID')或item.set( 'ID',11 )

這樣

Uncaught TypeError: Object # has no method 'get'

異常

這個項目不是Ember的對象?所以這個項目是什麼?

有人能告訴我如何改變「itme.id的價值..

萬分感謝

+1

裏面是什麼self.resultList?我認爲數組包含普通的JS對象和Ember對象。 – mavilein

回答

21

您可以使用Ember.set(yourObject, propertyName, value);Ember.get(yourObject, propertyName);安全設置和獲取屬性。

你的情況:

self.resultList.forEach(function(item, index, enumerable) { 
    Ember.set(item, "id", 11); 
    Ember.get(item, "id"); 
}); 
+0

它不工作.. – seagod

+0

收到任何錯誤? –

+0

這樣的異常:您必須使用Ember.set()來訪問此屬性(of [object Object]) – seagod

0

在我來說,我做了這樣

//In my controller I've defined the array 
 
displayInfoCheckboxes: [ 
 
    { 
 
     turnover: { 
 
     label: "Turnover", 
 
     value: 1, 
 
     propertyName: "turnover" 
 
     } 
 
    }, { 
 
     pl: { 
 
     label: "P&L", 
 
     value: 1 
 
     } 
 
    } 
 
] 
 

 
//and in my handler I passed the full string path to the property in the set method 
 

 
let displayInfoCheckboxes = this.get('displayInfoCheckboxes'); 
 
let controller = this; 
 

 
displayInfoCheckboxes.forEach(function(items,index) { 
 
    for (var key in items) { 
 
    controller.set('displayInfoCheckboxes.' + index + '.' + key + '.value', false); 
 
    } 
 
})