1

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?

Community
  • 1
  • 1
MikiDi
  • 13
  • 4
  • Have you considered to just generate an unique id in your serializer? When you don't modify these models that should work nicely. Probably you don't want the same `x/y` pair twice, and so just concatenate them with a separator should be good. – Lux Oct 05 '16 at 12:53
  • Just a quick aside, this may be an uphill battle using ember data for chart data. We look into doing this and encountered nothing but pain. Tom Dale states that they just use simple ember.objects with fetch() for skylight http://discuss.emberjs.com/t/time-series-data-in-ember-data/6945/2 – David Duncan Oct 05 '16 at 20:42
  • 1
    Thanks for sharing that @DavidDuncan , for the time being I'll just add an `id` to each point as @Lux suggested, but I'm very interested in hearing experiences with ember+timeseries data ... – MikiDi Oct 06 '16 at 20:50
  • @MikiDi We work with time series and in our app `Point` is not a model because it is not needed for our case and because of performance reasons. As you comment, we set the property the `dataset` as `attr()`. – ppcano Sep 25 '18 at 10:31

1 Answers1

0

You can use ember-data-model-fragments addon: https://github.com/lytics/ember-data-model-fragments

GUL
  • 1,175
  • 1
  • 13
  • 22