mercredi 5 octobre 2016

ember-data embedded objects without id

I can't seem to find a way to use a model for embedded objects that have no id in ember-data. This answer on how to deal with embedded objects by Jehuda Katz comes near to what I'm looking for, although I keep getting

Assertion Failed: You must include an 'id' for record in an object passed to 'push'

which seems to be expected behaviour when reading the EmbeddedRecordsMixin documentation which states:

Records without an id property are not considered embedded records, model instances must have an id property to be used with Ember Data.

JSON received from the REST-API looks like this:

{
  'line': {
    _id: 5,
    name: "my line",
    opts: {...},
    dataset: [
      {x: 1473167369522, y: 5},
      {x: 1473167469522, y: 6},
      {x: 1473167569522, y: 7}
    ]
  }
}

app/models/line.js

export default DS.Model.extend({
  name: DS.attr('string'),
  opts: DS.attr(),
  dataset: DS.hasMany('point')
});

app/models/point.js

export default DS.Model.extend({
  x: DS.attr('custom'),
  y: DS.attr('')
});

app/serializers/line.js

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    dataset: { embedded: 'always'}
  }
});

How should I go about tackling this problem?
I have already considered just making the property that contains the array of nested objects DS.attr() and abandoning the nested model altogether but this won't work either as I need some custom (de-)serialization on one of the embedded object's properties

related (although the Ember API has gone through quite some change):
- How to access nested object in json with Ember data
- Ember data and mongodb state?




Aucun commentaire:

Enregistrer un commentaire