DS.RESTSerializer#modelNameFromPayloadType()

modelNameFromPayloadType (payloadType) Stringpublic

Inherited from DS.JSONSerializer but overwritten in addon/serializers/rest.js:861

modelNameFromPayloadType can be used to change the mapping for a DS model name, taken from the value in the payload.

Say your API namespaces the type of a model and returns the following payload for the post model, which has a polymorphic user relationship:

// GET /api/posts/1
{
  "post": {
    "id": 1,
    "user": 1,
    "userType: "api::v1::administrator"
  }
}

By overwriting modelNameFromPayloadType you can specify that the administrator model should be used:

app/serializers/application.js
import DS from "ember-data";

export default DS.RESTSerializer.extend({
  modelNameFromPayloadType(payloadType) {
    return payloadType.replace('api::v1::', '');
  }
});

By default the modelName for a model is its name in dasherized form. Usually, Ember Data can use the correct inflection to do this for you. Most of the time, you won't need to override modelNameFromPayloadType for this purpose.

Also take a look at payloadTypeFromModelName to customize how the type of a record should be serialized.

Parameters:

payloadType String
type from payload

Returns:

String
modelName
doc_EmberJs
2016-11-30 16:50:36
Comments
Leave a Comment

Please login to continue.