DS.JSONAPISerializer#modelNameFromPayloadType()

modelNameFromPayloadType (payloadType) Stringpublic

Inherited from DS.JSONSerializer but overwritten in addon/serializers/json-api.js:630

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:

// GET /api/posts/1
{
  "data": {
    "id": 1,
    "type: "api::v1::post"
  }
}

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

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

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

By default the modelName for a model is its singularized 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:49:43
Comments
Leave a Comment

Please login to continue.