我有一个调查应用程序,其中包含存储问题及其选择的模型:
/models/question.js
export default DS.Model.extend({
text: DS.attr(),
choices: DS.attr(),
});
使用mirage-fixtures
存储以下问题:
export default [{
id: '1',
text: 'What is your name\'s first letter?',
choices: ['A', 'B', 'C', 'D'],
}, { ... }
我还有另一种存储记录的模型:
/models/answer.js
export default DS.Model.extend({
submittedAt: DS.attr(),
questions: DS.hasMany('question')
});
/routes/answer.js
export default Ember.Route.extend({
model() {
return this.get('store').createRecord('answer', { submittedAt: new Date() });
},
});
此外,我使用ember-radio-button,作为处理单选按钮的更简单方法。在一个调用问题的嵌套路由中挂钩问题组件。
/routes/answer/question.js
export default Ember.Route.extend({
model(params) {
return this.get('store').find('question', params.question_id)
}
});
/templates/answer/question.hbs
{{model.text}}
{{#each model.choices as |choice|}}
{{#radio-button
value=choice
groupValue=question
changed=selected
}}
{{choice}}
{{/radio-button}}
{{/each}}
/components/a-question.js
export default Ember.Component.extend({
actions: {
selected(choice) {
console.log('Current choice is: ', choice);
this.set('question', choice);
}
}
});
那么如何在答案数据中存储每个问题的选定选项?