modelNameFromPayloadKey (key) String
This method is used to convert each JSON root key in the payload into a modelName that it can use to look up the appropriate model for that part of the payload.
For example, your server may send a model name that does not correspond with the name of the model in your app. Let's take a look at an example model, and an example payload:
app/models/post.js
import DS from 'ember-data'; export default DS.Model.extend({ });
{ "blog/post": { "id": "1 } }
Ember Data is going to normalize the payload's root key for the modelName. As a result, it will try to look up the "blog/post" model. Since we don't have a model called "blog/post" (or a file called app/models/blog/post.js in ember-cli), Ember Data will throw an error because it cannot find the "blog/post" model.
Since we want to remove this namespace, we can define a serializer for the application that will remove "blog/" from the payload key whenver it's encountered by Ember Data:
app/serializers/application.js
import DS from 'ember-data'; export default DS.RESTSerializer.extend({ modelNameFromPayloadKey: function(payloadKey) { if (payloadKey === 'blog/post') { return this._super(payloadKey.replace('blog/', '')); } else { return this._super(payloadKey); } } });
After refreshing, Ember Data will appropriately look up the "post" model.
By default the modelName for a model is its name in dasherized form. This means that a payload key like "blogPost" would be normalized to "blog-post" when Ember Data looks up the model. Usually, Ember Data can use the correct inflection to do this for you. Most of the time, you won't need to override modelNameFromPayloadKey
for this purpose.
Parameters:
-
key
String
Returns:
-
String
- the model's modelName
Please login to continue.