dimanche 23 juillet 2017

createRecord for relationship of unsaved record

I have the following scenario:

1- Parent route that creates a record for multi-page form

routes/feedback.js
model() {
  return this.get('store').createRecord('feedback');
},

2- child route that return two models, question is non-related model that return multiple questions showing in dynamic route. And answer which creates a record that answer these questions in its page

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- Answer has a belongsTo relationship feedback

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- data passed from a question to an answer record through controller

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,
    // review: should relationship go here?
  })
  answer.save()

controllers/feedback.js
newModel: null,
actions: {
  save(newModel) {
    newModel.save()
  }
}

So how the relationship should be assigned if feeback doesn't have an ID yet?

Also can I hold the answer saving till the newModel for feedback is saved? Actually, I tried to bubble getChoice action to parent controller but the same problem of null record.




Aucun commentaire:

Enregistrer un commentaire