Loading a snake case field with ember data using the JSONAPI adapter

If I have a field that in the case of a snake returns from api, how can I define this field in the model? I am using JSONAPIAdapter. Fields seem to be one word that works great, but snake case fields return as undefined.

This is how I defined it 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 here is how json returns from the API:

1: {
  id: "96"
  type: "contact_infos"
  attributes: {
    typecode_desc: "E-mail address"
    contactnum: "billybear@yahoo.com"
    email: 1
  }
}

However, in the ember inspector, it typecode_descreturns as undefined. Is there something I need to do to tell ember that the fields will return as is the case with a snake?

+4
source share
1 answer

keyForRelationship JSON API. :

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);
  }
});
+4

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


All Articles