我对Ember有一些问题,我不太明白。我有一个数组是这样的:Emberjs - 修改阵列
var device.images = [
0: {
url: "url",
key: "key"
},
1: "url2",
2: "url3",
3: "url4"
];'
而且在我的控制器,一个地图里我尝试从数组修改每一个项目这样做:
device.images[idx] = image;
image
是一个新的对象,看起来就像我的数组中的第一项。但是,我总是得到断言失败:
Assertion Failed: You must use Ember.set() to access this property (of [object Object]...
我该如何解决呢?因为这通常不适用?
[编辑]下面是完整的代码:
loadGallery: function(device) {
var that = this;
var images = device.images.map(function(item, idx) {
return new Ember.RSVP.Promise(function(resolve) {
if (item.url) {
resolve(item);
}
else {
resolve(that.imageStore.readImage(that, item));
}
})
.then(function(image) {
device.images.set(idx, image);
that.set('selectedItem.images', device.images);
return image;
});
});
return Ember.RSVP.Promise.all(images);
},
我试过了,但是我得到了'错误:断言失败:错误:断言失败:不能用0键调用set .'。我的idx在这里是来自我的地图的迭代(我编辑了主帖)。这应该可行......:另外,设备只是一些从点击事件中检索的数据(例如,用户点击了哪个设备)。 – SuperMarco
嗯,再看一遍,似乎如果你使用普通数组,你应该可以使用'array [x] = y'构造。 [JSBin](http://emberjs.jsbin.com/vusomuni/3/edit)。你的情况下'device.images'究竟是什么? – panta82
我在我的模型中加载了产品列表,每个产品都有一些信息和一系列带有网址的图像。当使用点击产品时,它会触发我的处理程序'{{action'selectDevice'device}}',它将发送点击设备的对象。所以,基本上,设备是一个对象,device.images是设备对象中的一个数组。我只需要用对象替换数组中的每个项目。 – SuperMarco