How to access array data hidden in a DS.PromiseArray

限于喜欢 提交于 2019-12-21 05:32:13

问题


This is a follow up to: accessing another models data in ember.js

I have a situation where I would like to filter a list with a chosen multi-select box. When sending the data, this.store.find('tag') always returns a DS.PromiseArray. The Ember.Select seems to handle this fine, but the chosen multi select doesn't seem to like it. I have seen something like this:

this.store.find('tag').then(function(items) {
   return items.map(function(item){
      return [item.get('id'), item.get('name')]
   })
})

but I always seem to get a typeerror{} on the map function...

Here is a jsfiddle that outlines the problem: http://jsfiddle.net/viciousfish/TEZjW/

Bonus Points! demo shows the chosen select as a single select (for clarity). I would like to use this as a multi select, which can be set by setting multiple: true in App.MultipleSelect

Update here is another jsfiddle with what I think should work, but doesn't seem to! http://jsfiddle.net/viciousfish/FZ6yw/1/

And even further, this fiddle shows that the .then should work to deconstruct the promiseArray http://jsfiddle.net/marciojunior/DGT5L/


回答1:


Here is a functional jsfiddle http://jsfiddle.net/FZ6yw/2/

I moved the promise code to the afterModel hook of the route, as is promise friendly https://gist.github.com/machty/5723945

I also changed the map function, you were returning arrays and in my code it returns objects

App.AssetsRoute = Ember.Route.extend({
  model: function () {
    return this.store.find('asset');
  },
  afterModel: function () {
    var self = this;
    return this.store.find('tag').then(function(items) {
       var allTags = items.map(function(item){
           return {id:item.get('id'), name:item.get('name')};
       });
       self.controllerFor('tags').set('content', allTags);
    })
  },
});


来源:https://stackoverflow.com/questions/19413663/how-to-access-array-data-hidden-in-a-ds-promisearray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!