Is there a way to use a different property name than in my JSON?

I have the following JSON (simple example):

{ id: 101, firstName: "John", surname: "Doe" } 

But I want my model to use lastName instead of surname . Something like this might be:

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

I could have sworn that somewhere I saw something showing how to do it, but for life it could not find me. I also did not find anything obvious in the ember data source.

I tried the key , name , id , alias and map settings in the attribute parameters, but none of them do the trick. Is there any way to do this?

+4
source share
1 answer

You must do this through the REST adapter . The documentation includes an example for displaying โ€œirregular keysโ€ under Underlined attribute names :

Irregular keys may appear on the adapter. If JSON has the key lastNameOfPerson , and the name of the required attribute is just lastName , notify the adapter:

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

In your case:

 DS.RESTAdapter.map('App.Person', { lastName: { key: 'surname' } }); 

It is also worth noting that Ember expects JSON to have first_name , while the model has firstName . Thus, it may also be necessary to explicitly configure.

+7
source

All Articles