18

I am really stuck with tons of problems caused by Ember-data and it lacks of embedded records support.

I have searched the entire web, most of the posts are outdated others are outdated + requires me to use 3rd party libraries or to wire up 300 lines of special code with lots of drawbacks.

I've no idea how to use embedded records with ember-data as it stands today?

edit: there is a new documentation now http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html

kfir124
  • 1,286
  • 4
  • 14
  • 27

1 Answers1

31

Using the ActiveModelSerializer you can include the EmbeddedRecordsMixin which allows you to use embedded records. (In the canary versions, 1.0 beta 9+, you can use the JsonSerializer/RESTSerializer as well)

Serializer

App.ColorSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    foos: {embedded: 'always'}
  }
});

Models

App.Color = DS.Model.extend({
  color: DS.attr(),
  foos: DS.hasMany('foo')
});

App.Foo = DS.Model.extend({
  name: DS.attr()
});

JSON

{
 colors:[
  {
    id: 1,
    color: "red",
    foos:[
      {
        id:1,
        name:'something 1'
      },
      {
        id:2,
        name:'something 2'
      }
    ]
  },
  ...

http://emberjs.jsbin.com/qagalabaso/1/edit

For the RESTSerializer and JsonSerializer it follows the same pattern

App.ColorSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    foos: {embedded: 'always'}
  }
});

http://emberjs.jsbin.com/lesiwebobi/1/edit

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • 9
    Thanks @kingpin2k without you 90% of Ember questions would remain unanswered :), do i have to make a serilaizer for each model? or i can set some generic global embedded serilaizer – kfir124 Jun 15 '14 at 06:16
  • that's a great question, I'd imagine you could just do an ApplicationSerializer, and just list all of the types that are embedded in the `attrs` hash. – Kingpin2k Jun 15 '14 at 06:21
  • 3
    Excellent! I think that Ember docs totally miss all the embedded part – kfir124 Jun 15 '14 at 07:42
  • How can i make it work with the FixtureAdapter? seems like it bypass transformers and serializers – kfir124 Jun 15 '14 at 12:05
  • Yeha, unfortunately that mixin only works with the active model serializer, I believe it's planned to make it available for the rest serializer, but hasn't happened yet. Additionally you can't use pushPayload to sideload the record. Really the only working use case is the active model serializer, and data from the server (or mocked data) – Kingpin2k Jun 15 '14 at 15:47
  • I'm trying to use this for a belongsTo relationship but it's not working, is that possible? – DEfusion Sep 19 '14 at 15:10
  • Can you POST embedded records to the server? – vasilakisfil Oct 13 '14 at 11:01
  • Documentation is now up to date: http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html – Benjamin Netter Oct 29 '14 at 11:02
  • @Kingpin2k please setup same JSBIN example for `Ember: 1.12.1 & Ember Data: 1.13.4` - In this configuration I can't get embedded records working! – Marecky Aug 19 '15 at 00:36
  • Are you sure those two versions are compatible? – Kingpin2k Aug 19 '15 at 13:42
  • any workaround to allow the use of RESTSerializer? I'm getting "Cannot read property 'replace' of undefined". – Breno Inojosa Nov 11 '15 at 13:36