首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对Ember-Data结果使用ember-power-select

如何对Ember-Data结果使用ember-power-select
EN

Stack Overflow用户
提问于 2015-12-07 20:37:41
回答 2查看 566关注 0票数 0

大家好,

我试图将模型数据从store.findAll()调用加载到ember power-select组件中。来自findAll()的数据可以很好地加载到组件中,但是我需要设置最初选择的项,而且我似乎没有做任何事情。

EN

回答 2

Stack Overflow用户

发布于 2015-12-08 00:25:51

您的解决方案将询问服务器两次。检查此解决方案(es6语法)

代码语言:javascript
复制
setupController(controller, model) {
  controller.set('model', model);
  this.store.findAll('club').then(clubs => {
    controller.set('clubs', clubs);
    // i am not sure if the function is called filter, but it should be close enough
    return clubs.filter(club => clubs.get('id') === model.get('clubId')).get('firstObject');
  }).then(selectedClub =>  controller.set('selectedClub', selectedClub)).catch(err => {
    console.log("Error:", err);
  });
}
票数 1
EN

Stack Overflow用户

发布于 2015-12-07 20:37:41

诀窍是在使用store.query将元素从DS缓存中拉出后,使用带有'firstObject‘的get()方法。

这就是我最终要做的--如果有人有更优雅的方式来做这件事,我很乐意听到……

代码语言:javascript
复制
setupController: function(controller, model) {
  var _this = this;

  controller.set('model',model);

  _this.store.findAll('club')
  .then(function(results){
     controller.set('clubs', results);
     return _this.store.query('club', {id:model.get('clubId')});
  })
  .then(function(result){
     controller.set('selectedClub', result.get('firstObject'));
  })
  .catch(function(err){
    console.log("Error:",err);
  });

},

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34133738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档