所以我有一个ember-cli 1.13.8应用程序,我想在没有答案的情况下查看问题。目前,我可以根据属性过滤搜索。例如,我可以搜索问题的“内容”,因为这是对象的属性。
模特:
回答:
export default DS.Model.extend({
...
content: DS.attr(),
question: DS.belongsTo('question', { async: true })
});
的问题:
export default DS.Model.extend({
...
answers: DS.hasMany('answer', { async: true }),
});
例如,这可用于查询当前模型的属性(问题):
model(query) {
return this.store.findAll('question').then(function(questions) {
return questions.filterBy("content", query);
});
}
但我无法根据模型属性的属性(在本例中为长度)进行过滤。我想根据那些没有答案的问题进行过滤。
model() {
return this.store.findAll('question').then(function(questions) {
return questions.filter(function(item, index, enumberable) {
return item.answers.length === 0;
})
});
}
另一次尝试:
model() {
this.store.findAll('question').filter('question', function(q) {
return q.get('answers.length') === 0;
})
}
我咨询过: Emberjs filter() versus filterProperty()
似乎不推荐使用filterProperty(),例如: http://www.kaspertidemann.com/how-to-filter-an-array-of-records-by-a-property-value-in-ember-js/没有帮助。
答案 0 :(得分:3)
问题在于答案关系尚未加载。我这样做的方法是让路由负责获取模型并让控制器负责显示数据。
在您想要过滤掉没有答案的问题的路线中:
model() {
return this.store.findAll('question').then( (questions) => {
let promiseArray = questions.map( (question) => {
return question.get('answer');
})
return Ember.RSVP.all(promiseArray);
});
}
在控制器中过滤模型:
filteredModels: Ember.computed('model.[]', function() {
return this.get('model').filter( (question) => {
return question.get('answers.length') === 0;
})
})