1

Pardon me for coming up with this title but I really don't know how to ask this so I'll just explain. Model: Group (has) User (has) Post Defined as:

// models/group.js
name: DS.attr('string'),

// models/user.js
name: DS.attr('string'),
group: DS.belongsTo('group')

// models/post.js
name: DS.attr('string'),
user: DS.belongsTo('user'),

When I request /posts, my server returns this embedded record:

{
  "posts": [
    {
      "id": 1,
      "name": "Whitey",
      "user": {
        "id": 1,
        "name": "User 1",
        "group": 2
      }
    }
  ]
}

Notice that the group didn't have the group record but an id instead.

With my serializers:

// serializers/user.js
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
        group: {embedded: 'always'}
    }
});

// serializers/post.js
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
        user: {embedded: 'always'}
    }
});

This expects that the User and Post model have embedded records in them. However, it entails a problem since in the json response doesn't have an embedded group record.

The question is, is there a way I can disable embedding records in the 3rd level?

Please help.

Melvin
  • 5,798
  • 8
  • 46
  • 55

1 Answers1

5

Here is a good article about serialization:

http://www.toptal.com/emberjs/a-thorough-guide-to-ember-data#embeddedRecordsMixin

Ember docs:

http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html

Basically, what it says is that you have 2 options 1) serialize and 2) deserialize. Those two have 3 options:

  1. 'no' - don't include any data,
  2. 'id' or 'ids' - include id(s),
  3. 'records' - include data.

When you write {embedded: 'always'} this is shorthand for: {serialize: 'records', deserialize: 'records'}.

If you don't want the relationship sent at all write: {serialize: false}.

The Ember defaults for EmbeddedRecordsMixin are as follows:

BelongsTo: {serialize:'id', deserialize:'id'}

HasMany: {serialize:false, deserialize:'ids'}

Taysky
  • 4,331
  • 2
  • 20
  • 28
  • 1
    Is it possible to selectively embed relationships on models? For example I have a situation when on a particular page I don't need video tags but on the details page I do. If I use `embedded: 'always'` then the serializer will always look for `videos.tags` object. – Andrei Stalbe Nov 26 '15 at 12:41
  • How to not include ember generated ids for subtypes in a belongs to relationship? – RamPrasadBismil Aug 12 '16 at 04:28