How to access array data hidden in a DS.PromiseArr

2019-03-31 13:01发布

问题:

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