DS.EmbeddedRecordsMixin#serializeBelongsTo()

serializeBelongsTo (snapshot, json, relationship)

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

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
doc_EmberJs
2016-11-30 16:49:34
Comments
Leave a Comment

Please login to continue.