Class Serializer
Extends: | EmberObject |
---|---|
Defined in: | ../serializer/addon/index.js:7 |
Module: | @ember-data/serializer |
normalize (typeClass, hash) Object
Module: | @ember-data/serializer |
---|
Defined in ../serializer/addon/index.js:127
- typeClass
- Model
- hash
- Object
- returns
- Object
The normalize
method is used to convert a payload received from your external data source into the normalized form store.push()
expects. You should override this method, munge the hash and return the normalized payload.
Example:
Serializer.extend({ normalize(modelClass, resourceHash) { let data = { id: resourceHash.id, type: modelClass.modelName, attributes: resourceHash }; return { data: data }; } })
normalizeResponse (store, primaryModelClass, payload, id, requestType) Object
Module: | @ember-data/serializer |
---|
Defined in ../serializer/addon/index.js:49
Available since v1.13.0
- store
- Store
- primaryModelClass
- Model
- payload
- Object
- id
- String|Number
- requestType
- String
- returns
- Object
- JSON-API Document
The normalizeResponse
method is used to normalize a payload from the server to a JSON-API Document.
http://jsonapi.org/format/#document-structure
Example:
Serializer.extend({ normalizeResponse(store, primaryModelClass, payload, id, requestType) { if (requestType === 'findRecord') { return this.normalize(primaryModelClass, payload); } else { return payload.reduce(function(documentHash, item) { let { data, included } = this.normalize(primaryModelClass, item); documentHash.included.push(...included); documentHash.data.push(data); return documentHash; }, { data: [], included: [] }) } } });
serialize (snapshot, options) Object
Module: | @ember-data/serializer |
---|
Defined in ../serializer/addon/index.js:85
- snapshot
- Snapshot
- options
- Object
- returns
- Object
The serialize
method is used when a record is saved in order to convert the record into the form that your external data source expects.
serialize
takes an optional options
hash with a single option:
-
includeId
: If this istrue
,serialize
should include the ID in the serialized object it builds.
Example:
Serializer.extend({ serialize(snapshot, options) { let json = { id: snapshot.id }; snapshot.eachAttribute((key, attribute) => { json[key] = snapshot.attr(key); }); snapshot.eachRelationship((key, relationship) => { if (relationship.kind === 'belongsTo') { json[key] = snapshot.belongsTo(key, { id: true }); } else if (relationship.kind === 'hasMany') { json[key] = snapshot.hasMany(key, { ids: true }); } }); return json; }, });
© 2020 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember-data/3.25/classes/Serializer/methods