Search code examples
jsonember.jsember-datajson-api

EmberJS doesn't populate camelCased attributes


EmberJS doesn't populate model attributes written in camelCase for some reason. I use JSONAPIAdapter (RESTAdapter is not an option). Here is my model:

import DS from 'ember-data';
export default DS.Model.extend({
    shortName: DS.attr('string'),
    fullName: DS.attr('string'),
    inn: DS.attr('string'),
    kpp: DS.attr('string'),
    checkingAccount: DS.attr('string'),
    correspodentAccount: DS.attr('string'),
    bik: DS.attr('string'),
});

My handlebars template:

{{#each model as |company|}}
        <tr class="companies-list__item">
            <td class="companies-list__property">{{company.shortName}}</td>
            <td class="companies-list__property">{{company.inn}}</td>
            <td class="companies-list__property">{{company.kpp}}</td>
            <td class="companies-list__property">{{company.checkingAccount}}</td>
            <td class="companies-list__property">{{company.correspodentAccount}}</td>
            <td class="companies-list__property">{{company.bik}}</td>
        </tr>
{{/each}}

Answer from server looks like this:

{
   type: 'company',
   id: 4,
   attributes: {
          shortName: 'Рога и копыта',
          fullName: 'ООО Рога и копыта',
          inn: '312312312',
          kpp: '767675572',
          checkingAccount: '3123145124',
          correspodentAccount: '6547598',
          bik: '76979356'
   },
 },

It does populate inn, kpp, bik, but doesn't use others. What's the problem?


Solution

  • When using the JSONAPIAdapter/Serializer, I think that the default for attributes in the JSON payload is dasherized (kabob-case). See https://guides.emberjs.com/v2.10.0/models/customizing-serializers/ you may need to make some customizations.