我有以下情况:
1-为多页表单创建记录的父路由
routes/feedback.js
model() {
return this.get('store').createRecord('feedback');
},
返回两个模型的子路径,问题是返回动态路径中显示的多个问题的非相关模型。答案创建了一个记录,在其页面中回答这些问题
routes/feedback/question.js
model(params) {
return Ember.RSVP.hash({
question: this.get('store').findRecord('question', params.question_id),
answer: this.get('store').createRecord('answer')
})
},
3-答案有belongsTo关系反馈
models/answer.js
choice: DS.attr(), // receives data from question through a controller
feedback: DS.belongsTo()
models/feedback.js
submittedAt: DS.attr(),
answers: DS.hasMany()
4-数据通过控制器从问题传递到答案记录
controllers/feedback/question.js
getChoice(choice) {
let answer = this.get('model.answer');
let questionId = this.get('model.question').get('id');
answer.setProperties({
choice: choice,
questionId: questionId,
// feedback: should relationship go here?
})
answer.save()
controllers/feedback.js
newModel: null,
actions: {
save(newModel) {
newModel.save()
}
}
那么如果反馈还没有ID,那么应如何分配关系呢?
我还可以保存答案,直到保存反馈的newModel为止?实际上,我试图将getChoice动作冒泡到父控制器,但同样存在空记录问题。
答案 0 :(得分:0)
您可以在feedback
中注入feedback/question
控制器并访问feedback
模型并使用它。
feedback: Ember.inject.controller('feedback')
和
let answer = this.get('model.answer');
let questionId = this.get('model.question').get('id');
let feedback = this.get('feedback').get('model');
answer.setProperties({
choice,
questionId,
feedback
})
- 或者要从反馈控制器获取反馈记录,您可以在反馈/问题路径中使用this.modelFor('feedback')
routes/feedback/question.js
model(params) {
return Ember.RSVP.hash({
question: this.get('store').findRecord('question', params.question_id),
answer: this.get('store').createRecord('answer'),
feedback: this.modelFor('feedback')
})
},
和
let answer = this.get('model.answer');
let questionId = this.get('model.question').get('id');
let feedback = this.get('feedback');
answer.setProperties({
choice,
questionId,
feedback
})