mercredi 2 décembre 2015

Sideloaded JSON data not being pulled into hasMany relationship with Ember Data

Note: Using Ember 2.2.0 and Ember Data 2.2.0

I have a JSON model that looks like this:

{
  "cupboard": {
    "id": "D",
    "items": ["PASTA_SAUCE_2"],
    "name": "Right-most",
  },
  "items": [{
    "id": "PASTA_SAUCE_2",
    "weight": 14,
    "name": "Preggo Marinara"
  }]
}

I believe this to be properly formatted for sideloading, and I have the corresponding data models defined like so:

models/cupboard.js

export default DS.Model.extend({
  name: DS.attr('string'),
  items: DS.hasMany('cupboard-item-ref')
});

models/cupboard-item-ref.js

export default DS.Model.extend({
  name: DS.attr('string'),
  weight: DS.attr('number')
});

I have also created a serializer to assist with the embedded-ness and formatting of the JSON:

serializers/cupboard.js

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  keyForRelationship: function(attr, relationship) {
    if (attr == 'items' && relationship == 'hasMany') {
      return 'items';
    }
    this._super(attr, relationship);
  },
  attrs: {
    items: {
      embedded: 'always'
    }
  }
});

However, when I load the JSON provided above, I end up with the cupboard properly parsed into a data model object, but the Ember Inspector shows a fulfilled, empty, PromiseManyArray:

enter image description here

I feel like I'm missing (or mistakenly including) a critical piece of my DS.RESTSerializer implementation.




1 commentaire: