2

does anyone know if there is a recent problem with camelcased properties.

I have a model like this:

var attr = DS.attr;

App.Users = DS.Model.extend({
    firstName: attr('string'),
    phone: attr('string'),
    email: attr('string')
});

In my template email and phone show up correctly but firstName doesnt appear. I checked the json file and everything seems to be fine. With all my other models the same problems appear, so i guess it has to do something with the camelCase.

MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
Alexander Hauer
  • 73
  • 2
  • 10
  • Shouldn't it be `firstName: DS.attr('string'), ...`? – MilkyWayJoe Mar 21 '13 at 15:50
  • 1
    It should definitely work. The json response from the server contains 'first_name' or 'firstName' ? – sly7_7 Mar 21 '13 at 15:59
  • if your json is giving you `firstName`, you'll have to work on your adapter/serializer to map it to the property in the Model – MilkyWayJoe Mar 21 '13 at 16:00
  • The json response from the server contains firstName. – Alexander Hauer Mar 21 '13 at 16:01
  • 1
    I think you'll have to do something like this: `DS.RESTAdapter.map('App.User', { firstName: 'firstName' });` and I think the model name should be in the singular. – MilkyWayJoe Mar 21 '13 at 16:04
  • What i don't understand then, why phone and email are shown correctly and just all my camelCased properties are failing ? – Alexander Hauer Mar 21 '13 at 16:08
  • 1
    because they have the exact same name in both your Model and the JSON response. Ember.js relies on naming [conventions](http://emberjs.com/guides/models/the-rest-adapter/#toc_json-conventions), and it expects that multiple-word-camel-case (e.g. `firstName`) model properties to be mapped to multiple-word-underscore-separated-lower-case (e.g. `first_name`) attributes on the JSON response. – MilkyWayJoe Mar 21 '13 at 16:18
  • @MilkyWayJoe Ready to put your comments together in an answer ? :) – sly7_7 Mar 21 '13 at 16:19
  • @sly7_7 I responded as comment 'cause it was mostly questions and hints, but there it is. – MilkyWayJoe Mar 21 '13 at 16:34
  • 1
    @MilkyWayJoe Yeah I understand, I think now your answer fits perfectly to his question. Thanks for taking time :) – sly7_7 Mar 21 '13 at 16:38

1 Answers1

8

Deprecation Notice: This answer is from 2013, and hasn't been touched up until now. This does not reflect JSON API Adapter but rather Ember-Data's RESTAdapter.

Ember.js relies on naming conventions, and it expects that multiple-word-camel-case (e.g. firstName) model properties to be mapped to multiple-word-underscore-separated-lower-case attributes (e.g. first_name) on the JSON response. If your JSON is giving you firstName, or anything that is not in this convention in a scenario that you do not control the backend API, you have the option of defining a map which tells your adapter to look for a specific key in the JSON response and map it to a property in a given Model.

You can do something like this:

DS.RESTAdapter.map('App.User', { 
    firstName: { key: 'firstName' },
    lastName: { key: 'familyName' }
});

Note that in the sample above I've added a lastName property which is not part of your own model. I've done that just to make it clear that you can map several properties at the same time, and its name in the JSON response can be anything, not necessarily the same name in a different casing.

MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
  • @MilkyWayJoe Is there a more current way of doing this? I get the error `TypeError: DS.RESTAdapter.map is not a function` when I load the page – Nathan Hanna Mar 04 '15 at 00:09
  • @NathanHanna sadly no. I have looked into it, but I'm not up to date with the framework for about 6 months now so I can't really help here :( – MilkyWayJoe Mar 11 '15 at 21:23
  • 1
    Currently the JSON API does no longer use the `underscore` but the `hyphen-minus` char. See for more info: http://jsonapi.org/recommendations/#naming. – Jacob van Lingen May 11 '16 at 08:11