如何过滤Ember数据存储已获取的结果?

时间:2014-05-04 16:06:24

标签: ember.js ember-data

我知道我能做到:

this.questions = this.store.find('FaqQuestion', {category: 42);

但是,如果我在网页上显示常见问题解答的多个部分,我会想:

this.questions = this.store.find('FaqQuestion');

但是,我如何根据类别过滤这些问题?

FaqQuestion是DS模型:

App.FaqQuestion = DS.Model.extend({
    question: DS.attr('string'),
    answer: DS.attr('string'),
    target: function () {
        return '#' + this.get('id');
    }.property('id')
});

我试过这个(在ObjectController中):

deliveryQuestions: function () {
    return this.questions.filterProperty('category', 42);
}.property('deliveryQuestions')

各种渗透无济于事。

2 个答案:

答案 0 :(得分:3)

您需要在模型上定义类别

记录

App.FaqQuestion = DS.Model.extend({
    question: DS.attr('string'),
    answer: DS.attr('string'),
    category: DS.attr(),
    target: function () {
        return '#' + this.get('id');
    }.property('id')
});

过滤已获取的记录

var coolFaqsCollection = this.store.filter('faqQuestion', function(record){
  return record.get('category') == 42;
});

实施例

http://emberjs.jsbin.com/OxIDiVU/445/edit

答案 1 :(得分:1)

您应该在问题模型中定义类别关系

App.Category = DS.Model.extend({

});

App.Question = DS.Model.extend({
    category: DS.belongsTo('category'),
    answer: DS.attr('string')
});

一旦您提取了问题内容,​​您至少有两种可能性根据特定类别ID过滤内容。

this.controller.filter(function(record){
   return record.get('category.id') === "42";
});

this.get('store').filter('question', function(record){
   return record.get('category.id') === "42";  
});

检查demo example

相关问题