【发布时间】:2014-10-08 22:40:39
【问题描述】:
我正在尝试过滤 ember-data 'hasMany' 字段的内容。我的模型有一些子问题,我想将其过滤到控制器上的属性“childOptions”中并使用
显示在模板中{{#each childOptions}}stuff{{/each}}
当我把它放在我的控制器上时,它可以工作,并且每个都会迭代适当的值:
childOptions: Ember.computed.filterBy('model.subquestions', 'surveyQuestionType.name', 'childOption'),
但是当我这样做时,什么都没有显示。
childOptions: Ember.computed.filter('model.subquestions', function(subquestion) {
return subquestion.get('surveyQuestionType.name') === 'childOption';
}),
“surveyQuestionType”是一个 DS.belongsTo,存在于“子问题”的模型中,并且具有“名称”属性。
我想了解为什么 'filterBy' 方法有效,而 'filter' 方法无效(这样我以后可以使用 'filter' 进行更复杂的查询)。我认为这与 Promise 以及我在 filter 函数中使用的 subquestion.get('property') 语法有关。
编辑:
这是模型:
App.SurveyQuestion = DS.Model.extend(Ember.Validations.Mixin, {
surveyQuestionType: DS.belongsTo('surveyQuestionType', { async: true }),
display: DS.belongsTo('surveyQuestionDisplay', { async: true, inverse: 'surveyQuestion' }),
sortOrder: DS.attr('number'),
parent: DS.belongsTo('surveyQuestion', { async: true, inverse: 'subquestions' }),
parentDependencyCriteria: DS.attr('string'),
required: DS.attr('boolean'),
surveySections: DS.hasMany('surveySectionQuestion', { async: true, inverse: 'surveyQuestion' }),
subquestions: DS.hasMany('surveyQuestion', { async: true, inverse: 'parent' })
});
【问题讨论】:
-
subquestion.get('surveyQuestionType.name')返回undefined,我很确定这是问题所在,但我不知道如何解决!如果我在模板中做一个简单的{{#each subquestions}}{{surveyQuestionType.name}}{{/each}},我可以看到该属性就好了。
标签: ember.js ember-data