I have the following models:
App.Publication = DS.Model.extend({ title: DS.attr('string'), bodytext: DS.attr('string'), author: DS.belongsTo('author') }); App.Author = DS.Model.extend({ name: DS.attr('string') });
And the following json data:
{ "publications": [ { id: '1', title: 'first title', bodytext: 'first body', author_id: 100 }, { id: '2', title: 'second title', bodytext: 'second post', author_id: 200 } ]; }
In Ember Data RC12, this worked (you can specify author_id OR author in json and the publication will always contain the correct author).
In Ember Data 1.0.0, this no longer works; author is always null.
In some documents, I found that - since I use "author_id" in json data (and not just for the author) - I need to specify a key in the model; Thus:
author: DS.belongsTo('author', { key: 'author_id' })
This, however, does not work; the author in the publication remains invalid.
The only solution that I see at the moment is to implement a custom serializer and override author_id for the author (via normailzeId); I cannot change my internal data structure ... this way:
App.MySerializer = DS.RESTSerializer.extend({ //Custom serializer used for all models normalizeId: function (hash) { hash.author = hash.author_id; delete hash.author_id; return hash; } });
Is this correct above?