如何访问隐藏在DS.PromiseArray中的数组数据 [英] How to access array data hidden in a DS.PromiseArray

查看:90
本文介绍了如何访问隐藏在DS.PromiseArray中的数组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是后续的:访问ember.js中的其他模型数据

我有一种情况,我想使用选择的多选框过滤列表。发送数据时, this.store.find('tag')始终返回一个DS.PromiseArray。 Ember.Select似乎处理这个罚款,但选择的多选不似乎喜欢它。我看到这样的东西:

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')]
   })
})

但我总是似乎在地图函数上得到一个typeerror {} ...

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

这是一个概述这个问题的jsfiddle:
http: //jsfiddle.net/viciousfish/TEZjW/

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

奖金积分!演示将选择的选择显示为单个选择(for明晰)。我想使用这个作为一个多选择,可以通过在 App.MultipleSelect 中设置 multiple:true

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

更新这里是另一个jsfiddle,我认为应该工作,但似乎没有! http://jsfiddle.net/viciousfish/FZ6yw/1/

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

更进一步,这个小提琴显示.then应该可以解构promiseArray http://jsfiddle.net/marciojunior/DGT5L/

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

推荐答案

这是一个功能性的jsfiddle http://jsfiddle.net/FZ6yw/2/

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

我移动了对于路由的afterModel钩子的承诺代码,承诺友好 https://gist.github.com/machty/5723945

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

我还改变了map函数,你返回的数组,在我的代码中返回对象

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

这篇关于如何访问隐藏在DS.PromiseArray中的数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆