DS.JSONSerializer#normalize()

normalize (typeClass, hash) Object

Inherited from DS.Serializer but overwritten in addon/serializers/json.js:491

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

export default DS.JSONSerializer.extend({
  normalize: function(typeClass, hash) {
    var fields = Ember.get(typeClass, '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:

typeClass DS.Model
hash Object

Returns:

Object
doc_EmberJs
2016-11-30 16:49:51
Comments
Leave a Comment

Please login to continue.