Reputation: 401
If I have a field that is in snake case coming back from the api, how should I define that field in the model? Im using the JSONAPIAdapter. It seems like fields that is one word work fine, but snake case fields come back as undefined.
This is how I have it defined in my model:
import DS from 'ember-data';
export default DS.Model.extend({
typecode_desc: DS.attr('string'),
contactnum: DS.attr('string'),
email: DS.attr('number'),
individual: DS.belongsTo('individual', {async: false})
});
And this is how the json comes back from the API:
1: {
id: "96"
type: "contact_infos"
attributes: {
typecode_desc: "E-mail address"
contactnum: "[email protected]"
email: 1
}
}
However, in the ember inspector, typecode_desc
comes back as being undefined. Is there something I need to do to tell ember that fields will come back as being snake case?
Upvotes: 4
Views: 812
Reputation: 152
You need to define keyForRelationship
in your JSON API serializer. It will look something like this:
import DS from 'ember-data';
import Ember from 'ember';
export default DS.JSONAPISerializer.extend({
keyForAttribute: function(attr) {
return Ember.String.underscore(attr);
},
keyForRelationship: function(attr) {
return Ember.String.underscore(attr);
}
});
Upvotes: 4