0

UPDATE: Below is an original post regarding Ember Data Relationships that I believe are more related to a problem with the EmberFire adapter and how hasMany array records are handled. It seems the local store in Ember Data is updating appropriately, however these records are not being pushed into the appropriate Firebase database after the store is updated.

I've added the emberfire and firebase tags to hopefully find an answer in that community. Any help is appreciated and I'm happy to provide additional information as needed.


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:

   {{#each model.general.questions as |question|}}

      {{#paper-item class="md-1-line"}}
        <p class="question">
          {{question.detail}}
        </p>
        <p class="option">
          {{#if (eq question.type "text")}}
            {{paper-input value=question.id onChange=(action (mut question.id))}}
          {{/if}}
        </p>

      {{/paper-item}}

{{#each model.general.questions as |question|}} 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.

1 Answer 1

1

This issue seems to be related to the way EmberFire responds to models with a belongsTo and hasMany relationships where the "parent" model with a hasMany reference to the child models does not update automatically when a child model is created.

An issue has been submitted on GitHub to continue investigating how to resolve this issue: https://github.com/firebase/emberfire/issues/412

1
  • I added some context in the Github issue
    – tstirrat
    Jul 20, 2016 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.