serializeBelongsTo (snapshot, 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/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
Please login to continue.