mercredi 25 février 2015

How do I specify a mapping from an Ember model to JSON?

I have an API that returns:



{id: 1, "other-property": 100}


My Ember Data object is defined:



MyApp.MyThing = DS.Model.extend({
extId: DS.attr('number'),
otherProperty: DS.attr('number')})


It looks like I can't use id as an attribute in an ember model, and using non-identifier field names will cause trouble. So the guide suggests I write a normalizeHash function.



App.MyThingSerializer = DS.RESTSerializer.extend({
normalizeHash: {
id: function(hash) {
hash.appId = hash.id;
delete hash.id;
return hash;
},
otherProperty: function(hash) {
hash.otherProperty = hash['other-property'];
delete hash['otherProperty'];
return hash;
}
}
});


It doesn't suggest how to re-serialize when sending back over an API. How do I do this?


Is there a declarative way to do this? It seems very cumbersome.





Aucun commentaire:

Enregistrer un commentaire