API Docs for: 1.0.0-beta.4
Show:

DS.EmbeddedMixin Class

Module: mixins
Parent Module: ember-data

DS.EmbeddedMixin supports serializing embedded records.

To set up embedded records, include the mixin into a serializer then define embedded (model) relationships.

Below is an example of a per type serializer (post type).

App.PostSerializer = DS.RESTSerializer.extend(DS.EmbeddedMixin, {
  attrs: {
    author: {embedded: 'always'},
    comments: {embedded: 'always'}
  }
})

Currently only {embedded: 'always'} records are supported.

Methods

extractArray

(
  • store
  • primaryType
  • payload
)
Array

Extract embedded objects in an array when an attr is configured for embedded, and add them as side-loaded objects instead.

A payload with an attr configured for embedded records needs to be extracted:

{
  "post": {
    "id": "1"
    "title": "Rails is omakase",
    "comments": [{
      "id": "1",
      "body": "Rails is unagi"
    }, {
      "id": "2",
      "body": "Omakase O_o"
    }]
  }
}

Ember Data is expecting a payload with compound document (side-loaded) like:

{
  "post": {
    "id": "1"
    "title": "Rails is omakase",
    "comments": ["1", "2"]
  },
  "comments": [{
    "id": "1",
    "body": "Rails is unagi"
  }, {
    "id": "2",
    "body": "Omakase O_o"
  }]
}

The payload's comments attribute represents records in a hasMany relationship

Parameters:

  • store DS.Store
  • primaryType subclass of DS.Model
  • payload Object

Returns:

Array:

The primary array that was returned in response to the original query.

extractSingle

(
  • store
  • primaryType
  • payload
  • recordId
  • requestType
)

Extract an embedded object from the payload for a single object and add the object in the compound document (side-loaded) format instead.

A payload with an attribute configured for embedded records needs to be extracted:

{
  "post": {
    "id": 1
    "title": "Rails is omakase",
    "author": {
      "id": 2
      "name": "dhh"
    }
    "comments": []
  }
}

Ember Data is expecting a payload with a compound document (side-loaded) like:

{
  "post": {
    "id": "1"
    "title": "Rails is omakase",
    "author": "2"
    "comments": []
  },
  "authors": [{
    "id": "2"
    "post": "1"
    "name": "dhh"
  }]
  "comments": []
}

The payload's author attribute represents an object with a belongsTo relationship. The post attribute under author is the foreign key with the id for the post

Parameters:

  • store DS.Store
  • primaryType subclass of DS.Model
  • payload Object
  • recordId String
  • requestType 'find' | 'createRecord' | 'updateRecord' | 'deleteRecord'

Returns:

Object the primary response to the original request

serializeBelongsTo

(
  • record
  • json
  • relationship
)

Serialize belongsTo relationship when it is configured as an embedded object.

This example of an author model belongs to a post model:

Post = DS.Model.extend({
  title:    DS.attr('string'),
  body:     DS.attr('string'),
  author:   DS.belongsTo('author')
});

Author = DS.Model.extend({
  name:     DS.attr('string'),
  post:     DS.belongsTo('post')
});

Use a custom (type) serializer for the post model to configure embedded author

App.PostSerializer = DS.RESTSerializer.extend(DS.EmbeddedMixin, {
  attrs: {
    author: {embedded: 'always'}
  }
})

A payload with an attribute configured for embedded records can serialize the records together under the root attribute's payload:

{
  "post": {
    "id": "1"
    "title": "Rails is omakase",
    "author": {
      "id": "2"
      "name": "dhh"
    }
  }
}

Parameters:

  • record DS.Model
  • json Object
  • relationship Object

serializeHasMany

(
  • record
  • json
  • relationship
)

Serialize hasMany relationship when it is configured as embedded objects.

This example of a post model has many comments:

Post = DS.Model.extend({
  title:    DS.attr('string'),
  body:     DS.attr('string'),
  comments: DS.hasMany('comment')
});

Comment = DS.Model.extend({
  body:     DS.attr('string'),
  post:     DS.belongsTo('post')
});

Use a custom (type) serializer for the post model to configure embedded comments

App.PostSerializer = DS.RESTSerializer.extend(DS.EmbeddedMixin, {
  attrs: {
    comments: {embedded: 'always'}
  }
})

A payload with an attribute configured for embedded records can serialize the records together under the root attribute's payload:

{
  "post": {
    "id": "1"
    "title": "Rails is omakase",
    "body": "I want this for my ORM, I want that for my template language..."
    "comments": [{
      "id": "1",
      "body": "Rails is unagi"
    }, {
      "id": "2",
      "body": "Omakase O_o"
    }]
  }
}

To embed the ids for a related object (using a hasMany relationship):

App.PostSerializer = DS.RESTSerializer.extend(DS.EmbeddedMixin, {
  attrs: {
    comments: {embedded: 'ids'}
  }
})
{
  "post": {
    "id": "1"
    "title": "Rails is omakase",
    "body": "I want this for my ORM, I want that for my template language..."
    "comments": ["1", "2"]
  }
}

Parameters:

  • record DS.Model
  • json Object
  • relationship Object