1

Question 1: Why needs ember-data revision 11 an underscored version of the attribute name?

Question 2: Is it a problem, if the attribute names are identical (without the underscore)?

Ember-Link: http://emberjs.com/guides/models/the-rest-adapter/

The Model

App.Person = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),

    isPersonOfTheYear: DS.attr('boolean')
});

The JSON

{
   "person": {
        "first_name": "Barack",
        "last_name": "Obama",
        "is_person_of_the_year": true
    }
}

The Mapping

App.Person = DS.Model.extend({
    lastName: DS.attr('string')
});
DS.RESTAdapter.map('App.Person', {
    lastName: { key: 'lastNameOfPerson' }
});

Thanks to all responder! :)

Daniel
  • 111
  • 1
  • 8

1 Answers1

3

Question1: it's a convention in JS to have camel cased attributes. For the JSON payload, see this JSON Naming Convention

Not sure to understand your 2nd question.

Question2:

If you want to keep your JSON format, you have 2 options explained in the code of the serializer https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/serializer.js#L58

  • re-map them one-by-one using the adapter's map API
  • You can determine the attribute name used in the serialized form by implementing keyForAttributeName

In your case you probably want to have your own serializer that inherits from the RESTSerializer. See https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/rest_serializer.js#L4

keyForAttributeName would simply return the string.

Community
  • 1
  • 1
Cyril Fluck
  • 1,561
  • 7
  • 9
  • Hi Cyril, thanks for the hint. The point is: - I have a WCF application server side (camel case convention). - On client side JavaScript (camel case convention). - For Communication JSON (underscore convention is wished from Ember). And a convention converting makes less sense for me. Is it therefore possible, to keep the camel case convention for the whole system without getting Trouble on Ember side? – Daniel Mar 21 '13 at 06:59