2014-06-17 82 views
0

我对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); 
    }, 

回答

1

[编辑:第一次尝试是错误]

看来你应该能够使用item[index] = value结构。实例:

App.IndexRoute = Ember.Route.extend({ 
    resolveImage: function (that, item) { 
    return { 
     url: item, 
     key: "resolved" 
    }; 
    }, 
    model: function() { 
    var device = { 
     images: [ 
     {url: "a", key: "existing"}, 
     "b", 
     "c" 
     ] 
    }; 
    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.resolveImage(that, item)); 
     } 
     }) 
     .then(function(image) { 
     device.images[idx] = image; 
     return image; 
     }); 
    }); 
    return Ember.RSVP.hash({ 
     model: Ember.RSVP.Promise.all(images), 
     device: device 
    }); 
    }, 
    setupController: function (c, m) { 
    c.set("model", m.model); 
    c.set("device", m.device); 
    } 
}); 
<script type="text/x-handlebars" data-template-name="index"> 
    <h2>From model</h2> 
    <ul> 
    {{#each item in model}} 
     <li>{{item.url}} - {{item.key}}</li> 
    {{/each}} 
    </ul> 
    <h2>From device</h2> 
    <ul> 
    {{#each item in device.images}} 
     <li>{{item.url}} - {{item.key}}</li> 
    {{/each}} 
    </ul> 
    </script> 

JSBin here

除非一些bug其他地方在你的代码,剩下的唯一的解决办法是,你正在使用Ember.Dataimages是不是一个真正的数组,但,仅仅实现了一个或几个Ember的阵列/枚举混入的自定义类。 [...]构造是一种语法糖,它被硬编码为JavaScript,不可供库作者覆盖。

我帮不了你,因为我不使用Ember.Data。您可以尝试使用Ember.MutableArray界面中的显式阵列突变方法。例如:

device.images.replace(idx, 1, [image]); 

另外,使用object.set(key, value)结构(我的第一个建议),有需要提醒的是键值必须是字符串。例如:

device.images.set(String(idx), image); 

如果这些都不起作用,您可以尝试建立一个新的数组,而不是替换现有的成员。

+0

我试过了,但是我得到了'错误:断言失败:错误:断言失败:不能用0键调用set .'。我的idx在这里是来自我的地图的迭代(我编辑了主帖)。这应该可行......:另外,设备只是一些从点击事件中检索的数据(例如,用户点击了哪个设备)。 – SuperMarco

+0

嗯,再看一遍,似乎如果你使用普通数组,你应该可以使用'array [x] = y'构造。 [JSBin](http://emberjs.jsbin.com/vusomuni/3/edit)。你的情况下'device.images'究竟是什么? – panta82

+0

我在我的模型中加载了产品列表,每个产品都有一些信息和一系列带有网址的图像。当使用点击产品时,它会触发我的处理程序'{{action'selectDevice'device}}',它将发送点击设备的对象。所以,基本上,设备是一个对象,device.images是设备对象中的一个数组。我只需要用对象替换数组中的每个项目。 – SuperMarco