JSON API responses and ember model names

A quick question about the JSON API "type" response key answer matching the Ember model name.

If I have a model, say "models / photos.js" and I have a route like "/ photos", my JSON API response looks like this:

{
  data: [{
    id: "298486374",
    type: "photos",
    attributes: {
      name: "photo_name_1.png",
      description: "A photo!"
    }
  },{
    id: "298434523",
    type: "photos",
    attributes: {
      name: "photo_name_2.png",
      description: "Another photo!"
    }
  }]
}

I believe my model name should be singular, but this error appears

Assertion Failed: You tried to push data with a type 'photos' but no model could be found with that name

This, of course, is because my model is called "photo"

Now, there is a note in the JSON API specification that states: "This specification is independent of inflection rules, so the type value can be multiple or singular. However, the same value must be used consistently throughout the implementation."

So,

tl; dr " " -, , "" JSON API, ? , ?

+4
2

JSON API . .

modelNameFromPayloadKey , :

// as is
modelNameFromPayloadKey: function(key) {
  return singularize(normalizeModelName(key));
}

payloadKeyFromModelName , :

// as is
payloadKeyFromModelName: function(modelName) {
  return pluralize(modelName);
}

, API Ember Data JSON , JSONAPISerializer. Store.push , JSON API .

discussion:

"... ED camelCased , , / .

JSON API-/, , , jsonapi.org, . , .

, http://guides.emberjs.com/v2.0.0/models/pushing-records-into-the-store/ ... "

+5

pushPayload push. , ; " ".

+1

Source: https://habr.com/ru/post/1609378/


All Articles