AtomicSerializerMixin Class
Mixin that serializes uses Atomic Updates to only update fields that have been modified.
Atomic Updates can optionally include Optimistic Concurrency to ensure updates are not conflicting with other clients. See SolrAdapter.updateMode.
This implementation treats arrays as ordered and non-distinct.
A multiValued field will be updated if the order of items
changes or if any items are added or removed. The entire
array will be sent using a set operation to preserve order
and allow for duplicate values.
To treat multiValued fields as distinct sets, use
AtomicMultiValuedSerializerMixin
which will use add and remove operations.
Item Index
Methods
- applyTransforms
- extract
- extractArray
- extractCreateRecord
- extractDeleteRecord
- extractFind
- extractFindAll
- extractFindBelongsTo
- extractFindHasMany
- extractFindMany
- extractFindQuery
- extractMeta
- extractSave
- extractSingle
- extractUpdateRecord
- isAttributeModified
- keyForAttribute
- keyForRelationship
- normalize
- serialize
- serializeArrayAttribute
- serializeAttribute
- serializeBelongsTo
- serializeHasMany
- serializePolymorphicType
- transformFor
Properties
Methods
applyTransforms
-
type -
data
Given a subclass of DS.Model and a JSON object this method will
iterate through each attribute of the DS.Model and invoke the
DS.Transform#deserialize method on the matching property of the
JSON object. This method is typically called after the
serializer's normalize method.
Parameters:
-
typesubclass of DS.Model -
dataObject<p>The data to transform</p>
Returns:
<p>data The transformed data object</p>
extract
-
store -
type -
payload -
id -
requestType
The extract method is used to deserialize payload data from the
server. By default the JSONSerializer does not push the records
into the store. However records that subclass JSONSerializer
such as the RESTSerializer may push records into the store as
part of the extract call.
This method delegates to a more specific extract method based on
the requestType.
Example
var get = Ember.get;
socket.on('message', function(message) {
var modelName = message.model;
var data = message.data;
var type = store.modelFor(modelName);
var serializer = store.serializerFor(type.typeKey);
var record = serializer.extract(store, type, data, get(data, 'id'), 'single');
store.push(modelName, record);
});
Parameters:
Returns:
<p>json The deserialized payload</p>
extractArray
-
store -
type -
payload
extractArray is used to deserialize an array of records
returned from the adapter.
Example
App.PostSerializer = DS.JSONSerializer.extend({
extractArray: function(store, type, payload) {
return payload.map(function(json) {
return this.extractSingle(json);
}, this);
}
});
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>array An array of deserialized objects</p>
extractCreateRecord
-
store -
type -
payload
extractCreateRecord is a hook into the extract method used when a
call is made to DS.Store#createRecord. By default this method is
alias for extractSave.
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>json The deserialized payload</p>
extractDeleteRecord
-
store -
type -
payload
extractDeleteRecord is a hook into the extract method used when
a call is made to DS.Store#deleteRecord. By default this method is
alias for extractSave.
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>json The deserialized payload</p>
extractFind
-
store -
type -
payload
extractFind is a hook into the extract method used when
a call is made to DS.Store#find. By default this method is
alias for extractSingle.
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>json The deserialized payload</p>
extractFindAll
-
store -
type -
payload
extractFindAll is a hook into the extract method used when a
call is made to DS.Store#findAll. By default this method is an
alias for extractArray.
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>array An array of deserialized objects</p>
extractFindBelongsTo
-
store -
type -
payload
extractFindBelongsTo is a hook into the extract method used when
a call is made to DS.Store#findBelongsTo. By default this method is
alias for extractSingle.
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>json The deserialized payload</p>
extractFindHasMany
-
store -
type -
payload
extractFindHasMany is a hook into the extract method used when a
call is made to DS.Store#findHasMany. By default this method is
alias for extractArray.
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>array An array of deserialized objects</p>
extractFindMany
-
store -
type -
payload
extractFindMany is a hook into the extract method used when a
call is made to DS.Store#findMany. By default this method is
alias for extractArray.
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>array An array of deserialized objects</p>
extractFindQuery
-
store -
type -
payload
extractFindQuery is a hook into the extract method used when a
call is made to DS.Store#findQuery. By default this method is an
alias for extractArray.
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>array An array of deserialized objects</p>
extractMeta
-
store -
type -
payload
extractMeta is used to deserialize any meta information in the
adapter payload. By default Ember Data expects meta information to
be located on the meta property of the payload object.
Example
App.PostSerializer = DS.JSONSerializer.extend({
extractMeta: function(store, type, payload) {
if (payload && payload._pagination) {
store.metaForType(type, payload._pagination);
delete payload._pagination;
}
}
});
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
extractSave
-
store -
type -
payload
extractSave is a hook into the extract method used when a call
is made to DS.Model#save. By default this method is alias
for extractSingle.
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>json The deserialized payload</p>
extractSingle
-
store -
type -
payload
extractSingle is used to deserialize a single record returned
from the adapter.
Example
App.PostSerializer = DS.JSONSerializer.extend({
extractSingle: function(store, type, payload) {
payload.comments = payload._embedded.comment;
delete payload._embedded;
return this._super(store, type, payload);
},
});
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>json The deserialized payload</p>
extractUpdateRecord
-
store -
type -
payload
extractUpdateRecord is a hook into the extract method used when
a call is made to DS.Store#update. By default this method is alias
for extractSave.
Parameters:
-
storeDS.Store -
typesubclass of DS.Model -
payloadObject
Returns:
<p>json The deserialized payload</p>
isAttributeModified
-
snapshot -
key -
attribtue -
value -
previousValue
Determine if a value is modified from a previous value.
Parameters:
-
snapshotDS.Snapshot -
keyString -
attribtueDS.Attribute -
valueAnything -
previousValueAnything
Returns:
keyForAttribute
-
attr
Converts attributes to underscore to use conventional Solr field names
Parameters:
-
attrString
Returns:
<p>key</p>
keyForRelationship
-
key -
relationship
keyForRelationship can be used to define a custom key when
serializing relationship properties. By default JSONSerializer
does not provide an implementation of this method.
Example
App.PostSerializer = DS.JSONSerializer.extend({
keyForRelationship: function(key, relationship) {
return 'rel_' + Ember.String.underscore(key);
}
});
Returns:
<p>normalized key</p>
normalize
-
type -
hash
Normalizes a part of the JSON payload returned by the server. You should override this method, munge the hash and call super if you have generic normalization to do.
It takes the type of the record that is being normalized (as a DS.Model class), the property where the hash was originally found, and the hash to normalize.
You can use this method, for example, to normalize underscored keys to camelized or other general-purpose normalizations.
Example
App.ApplicationSerializer = DS.JSONSerializer.extend({
normalize: function(type, hash) {
var fields = Ember.get(type, 'fields');
fields.forEach(function(field) {
var payloadField = Ember.String.underscore(field);
if (field === payloadField) { return; }
hash[field] = hash[payloadField];
delete hash[payloadField];
});
return this._super.apply(this, arguments);
}
});
Parameters:
-
typesubclass of DS.Model -
hashObject
Returns:
serialize
-
snapshot -
options
Serialize only modified attributes for a snapshot using atomic update operations.
Parameters:
-
snapshotDS.Snapshot -
optionsObject
serializeArrayAttribute
-
snapshot -
key -
attribute -
value -
previousValue
Serialize an update to a multiValued field.
Parameters:
-
snapshotDS.Snapshot -
keyString -
attributeDS.Attribute -
valueAnything -
previousValueAnything
Returns:
serializeAttribute
-
snapshot -
json -
key -
attribute
Determines if an attribute can be serialized and if the value is dirty.
Parameters:
-
snapshotDS.Snapshot -
jsonObject -
keyString -
attributeDS.Attribute
serializeBelongsTo
-
record -
json -
relationship
serializeBelongsTo can be used to customize how DS.belongsTo
properties are serialized.
Example
App.PostSerializer = DS.JSONSerializer.extend({
serializeBelongsTo: function(record, json, relationship) {
var key = relationship.key;
var belongsTo = get(record, key);
key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo") : key;
json[key] = Ember.isNone(belongsTo) ? belongsTo : belongsTo.toJSON();
}
});
Parameters:
-
recordDS.Model -
jsonObject -
relationshipObject
serializeHasMany
-
record -
json -
relationship
serializeHasMany can be used to customize how DS.hasMany
properties are serialized.
Example
App.PostSerializer = DS.JSONSerializer.extend({
serializeHasMany: function(record, json, relationship) {
var key = relationship.key;
if (key === 'comments') {
return;
} else {
this._super.apply(this, arguments);
}
}
});
Parameters:
-
recordDS.Model -
jsonObject -
relationshipObject
serializePolymorphicType
-
record -
json -
relationship
You can use this method to customize how polymorphic objects are
serialized. Objects are considered to be polymorphic if
{polymorphic: true} is pass as the second argument to the
DS.belongsTo function.
Example
App.CommentSerializer = DS.JSONSerializer.extend({
serializePolymorphicType: function(record, json, relationship) {
var key = relationship.key,
belongsTo = get(record, key);
key = this.keyForAttribute ? this.keyForAttribute(key) : key;
json[key + "_type"] = belongsTo.constructor.typeKey;
}
});
Parameters:
-
recordDS.Model -
jsonObject -
relationshipObject
transformFor
-
attributeType -
skipAssertion
Parameters:
-
attributeTypeString -
skipAssertionBoolean
Returns:
<p>transform</p>
Properties
primaryKey
String
The primaryKey is used when serializing and deserializing
data. Ember Data always uses the id property to store the id of
the record. The external source may not always follow this
convention. In these cases it is useful to override the
primaryKey property to match the primaryKey of your external
store.
Example
App.ApplicationSerializer = DS.JSONSerializer.extend({
primaryKey: '_id'
});
Default: 'id'
versionFieldName
String
Field name to use for Solr Optimistic Concurrency. See Updating Parts of Documents.
Default: '_version_'
