大家好,
我试图将模型数据从store.findAll()调用加载到ember power-select组件中。来自findAll()的数据可以很好地加载到组件中,但是我需要设置最初选择的项,而且我似乎没有做任何事情。
发布于 2015-12-08 00:25:51
您的解决方案将询问服务器两次。检查此解决方案(es6语法)
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);
});
}发布于 2015-12-07 20:37:41
诀窍是在使用store.query将元素从DS缓存中拉出后,使用带有'firstObject‘的get()方法。
这就是我最终要做的--如果有人有更优雅的方式来做这件事,我很乐意听到……
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);
});},
https://stackoverflow.com/questions/34133738
复制相似问题