DS.EmbeddedRecordsMixin

DS.EmbeddedRecordsMixin Class

Defined in: addon/serializers/embedded-records-mixin.js:8

Module: ember-data

Using Embedded Records

DS.EmbeddedRecordsMixin supports serializing embedded records.

To set up embedded records, include the mixin when extending a serializer, then define and configure embedded (model) relationships.

Note that embedded records will serialize with the serializer for their model instead of the serializer in which they are defined.

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

app/serializers/post.js
import DS from 'ember-data';

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    author: { embedded: 'always' },
    comments: { serialize: 'ids' }
  }
});

Note that this use of { embedded: 'always' } is unrelated to the { embedded: 'always' } that is defined as an option on DS.attr as part of defining a model while working with the ActiveModelSerializer. Nevertheless, using { embedded: 'always' } as an option to DS.attr is not a valid way to setup embedded records.

The attrs option for a resource { embedded: 'always' } is shorthand for:

{
  serialize: 'records',
  deserialize: 'records'
}

Configuring Attrs

A resource's attrs option may be set to use ids, records or false for the serialize and deserialize settings.

The attrs property can be set on the ApplicationSerializer or a per-type serializer.

In the case where embedded JSON is expected while extracting a payload (reading) the setting is deserialize: 'records', there is no need to use ids when extracting as that is the default behavior without this mixin if you are using the vanilla EmbeddedRecordsMixin. Likewise, to embed JSON in the payload while serializing serialize: 'records' is the setting to use. There is an option of not embedding JSON in the serialized payload by using serialize: 'ids'. If you do not want the relationship sent at all, you can use serialize: false.

EmbeddedRecordsMixin defaults

If you do not overwrite attrs for a specific relationship, the EmbeddedRecordsMixin will behave in the following way:

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

Model Relationships

Embedded records must have a model defined to be extracted and serialized. Note that when defining any relationships on your model such as belongsTo and hasMany, you should not both specify async: true and also indicate through the serializer's attrs attribute that the related model should be embedded for deserialization. If a model is declared embedded for deserialization (embedded: 'always' or deserialize: 'records'), then do not use async: true.

To successfully extract and serialize embedded records the model relationships must be setup correcty. See the defining relationships section of the Defining Models guide page.

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

Example JSON payloads, Models and Serializers

When customizing a serializer it is important to grok what the customizations are. Please read the docs for the methods this mixin provides, in case you need to modify it to fit your specific needs.

For example review the docs for each method of this mixin: * normalize * serializeBelongsTo * serializeHasMany

_extractEmbeddedBelongsToprivate

Defined in addon/serializers/embedded-records-mixin.js:579

_extractEmbeddedHasManyprivate

Defined in addon/serializers/embedded-records-mixin.js:550

_extractEmbeddedRecordsprivate

Defined in addon/serializers/embedded-records-mixin.js:532

_normalizeEmbeddedRelationshipprivate

Defined in addon/serializers/embedded-records-mixin.js:602

normalize (typeClass, hash, prop) Object

Defined in addon/serializers/embedded-records-mixin.js:102

Normalize the record and recursively normalize/extract all the embedded records while pushing them into the store as they are encountered

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"
    }]
  }
}

Parameters:

typeClass DS.Model
hash Object
to be normalized
prop String
the hash has been referenced by

Returns:

Object
the normalized hash

removeEmbeddedForeignKey (snapshot, embeddedSnapshot, relationship, json)

Defined in addon/serializers/embedded-records-mixin.js:455

When serializing an embedded record, modify the property (in the json payload) that refers to the parent record (foreign key for relationship).

Serializing a belongsTo relationship removes the property that refers to the parent record

Serializing a hasMany relationship does not remove the property that refers to the parent record.

Parameters:

snapshot DS.Snapshot
embeddedSnapshot DS.Snapshot
relationship Object
json Object

serializeBelongsTo (snapshot, json, relationship)

Defined in addon/serializers/embedded-records-mixin.js:143

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/serializers/post.js
import DS from 'ember-data';

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  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:

snapshot DS.Snapshot
json Object
relationship Object

serializeHasMany (snapshot, json, relationship)

Defined in addon/serializers/embedded-records-mixin.js:242

Serializes hasMany relationships 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/serializers/post.js
import DS from 'ember-data;

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  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"
    }]
  }
}

The attrs options object can use more specific instruction for extracting and serializing. When serializing, an option to embed ids, ids-and-types or records can be set. When extracting the only option is records.

So { embedded: 'always' } is shorthand for: { serialize: 'records', deserialize: 'records' }

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

app/serializers/post.js
import DS from 'ember-data;

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    comments: { serialize: 'ids', deserialize: 'records' }
  }
})
{
  "post": {
    "id": "1"
    "title": "Rails is omakase",
    "body": "I want this for my ORM, I want that for my template language..."
    "comments": ["1", "2"]
  }
}

To embed the relationship as a collection of objects with id and type keys, set ids-and-types for the related object.

This is particularly useful for polymorphic relationships where records don't share the same table and the id is not enough information.

By example having a user that has many pets:

User = DS.Model.extend({
  name:    DS.attr('string'),
  pets: DS.hasMany('pet', { polymorphic: true })
});

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

Cat = Pet.extend({
  // ...
});

Parrot = Pet.extend({
  // ...
});
app/serializers/user.js
import DS from 'ember-data;

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    pets: { serialize: 'ids-and-types', deserialize: 'records' }
  }
});
{
  "user": {
    "id": "1"
    "name": "Bertin Osborne",
    "pets": [
      { "id": "1", "type": "Cat" },
      { "id": "1", "type": "Parrot"}
    ]
  }
}

Parameters:

snapshot DS.Snapshot
json Object
relationship Object

© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部