DS.JSONSerializer#serializePolymorphicType()

serializePolymorphicType (snapshot, json, relationship)

Defined in addon/serializers/json.js:1255

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

export default DS.JSONSerializer.extend({
  serializePolymorphicType: function(snapshot, json, relationship) {
    var key = relationship.key,
        belongsTo = snapshot.belongsTo(key);
    key = this.keyForAttribute ? this.keyForAttribute(key, "serialize") : key;

    if (Ember.isNone(belongsTo)) {
      json[key + "_type"] = null;
    } else {
      json[key + "_type"] = belongsTo.modelName;
    }
  }
});

Parameters:

snapshot DS.Snapshot
json Object
relationship Object
doc_EmberJs
2016-11-30 16:49:59
Comments
Leave a Comment

Please login to continue.