在我运行的测试应用程序中,我有员工的固定数据,包括姓名、部门等。目前,我的模型包含所有员工,我可以制作员工列表。但是,我想制作一个侧面组件,列出 1 名特定员工的信息(即单击的员工,作为工具提示)。如何仅将 1 名员工传递给我的组件?
我有一个操作,每当有人单击某个名称时就会更新属性。我想做一个计算属性来根据该名称查询模型,但我不确定如何过滤我的模型以仅返回一名员工。
actions: {
updateProfile(person) {
set(this, 'profile', person);
}
}
还有我的计算属性:
currentProfile: computed('profile', function(){
return this.model.find('person', {'name': get(this, 'profile')});
}),
有没有一种简单的方法可以从模型中返回我想要的 1 个对象?
解决方案
我需要过滤this
而不是模型本身。然后,我必须返回第一个对象,即使只有 1 个匹配项(这是主要问题)。
currentProfile: computed('profile', function(){
return this.filterBy('name', get(this, 'profile')).get('firstObject');
}),
最佳答案
试试这个:
currentProfile: computed('profile', function(){
var query = {'name': get(this, 'profile')};
return this.model.find('person', query).then(function(people){
return people.get('firstObject');
});
})
关于ember.js - 从我的 Ember 模型中返回 1 个对象/记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31642367/