mercredi 13 juillet 2016

Ember Data Relationships not updating correctly

I have two models that are defined with hasMany and belongsTo relationships. Here is the first "frameworks" model:

// -- Frameworks Model
import DS from 'ember-data';
const { attr, hasMany } = DS;

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    questions: { serialize: 'ids' }
  } 
});

export default DS.Model.extend({
  name: attr('string'),
  reports: hasMany('report'),
  questions: hasMany('question', {async: true}),
  includedFramework: attr('string'),
  reportOnlyOnce: attr('string', { defaultValue: false }),
  frameBuilder: attr('string', {defaultValue: "none"}),
  createdAt: attr('string'),
  createdBy: attr('string'),
  updatedAt: attr('string'),
  updatedBy: attr('string')
});

And the second "question" model:

// -- Question Model
import DS from 'ember-data';
const { attr, hasMany, belongsTo } = DS;

export default DS.Model.extend({
  framework: belongsTo('frameworks', {async: true}),
  detail: attr('string'),
  type: attr('string'),
  createdAt: attr('string'),
  createdBy: attr('string'),
  position: attr('number'),
  options: hasMany('option')
});

On my route, I pull in the model for the framework (in this case defining the model ID for testing):

import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    let id = params.report_id;
    var self = this;

    return Ember.RSVP.hash({
      report: this.store.find('report', id),
      general: this.store.find('frameworks', '-KITSAxjdE5TiVdk-aPC')
    })
  }
});

For some reason, this framework has an array stored in questions that has 4 of the 6 different "questions" that should belong to it. I don't know why 4 of the stored in the framework model as well and 2 of them did not. In my template for the page:

   

      
        <p class="question">
          
        </p>
        <p class="option">
          
            
          
        </p>

      

works great for the 4 questions that are actually stored in the framework model for this ID, but not the other 2. The other 2 questions do have the belongsTo relationship referencing the correct framework.

I have tried adding a serializer in the framework model (as you can see above) but there was no change.

I have also tried manually adding the question to the framework store when a new question is created with:

framework.get('questions').then(function(question) {
  return question.pushObject(newQuestion);
 })

but no change with that either.

Any thoughts are welcome, I'm happy to provide any other relevant code snippets as well.




Aucun commentaire:

Enregistrer un commentaire