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

mục như thế này: enter image description herecách thay đổi giá trị với mảng Ember.js forEach?

nếu item.id = 11; ngoại trừ như thế này:

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

nên item.get ('id') hoặc item.set ('id', 11)

ngoại lệ như thế này

Uncaught TypeError: Object # has no method 'get'

là mặt hàng này không phải là đối tượng của Ember?

ai đó có thể cho tôi biết làm thế nào để thay đổi 'giá trị itme.id của ..

Nhờ một triệu

+1

là gì bên trong self.resultList? Tôi nghĩ rằng mảng chứa cả các đối tượng JS thuần túy và các đối tượng Ember. – mavilein

Trả lời

21

Bạn có thể sử dụng Ember.set(yourObject, propertyName, value);Ember.get(yourObject, propertyName); để thiết lập một cách an toàn và nhận được tài sản.

Trong trường hợp của bạn:

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

nó không hoạt động .. – seagod

+0

Bất kỳ lỗi nào nhận được? –

+0

ngoại lệ như thế này: Bạn phải sử dụng Ember.set() để truy cập thuộc tính này (của [đối tượng đối tượng]) – seagod

0

Trong trường hợp của tôi, tôi đã làm điều đó theo cách này

//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); 
 
    } 
 
})

Các vấn đề liên quan