Reputation: 6396
How do i make this work with ember-data?
{"poll":{"id":1,"question":"lym features",
"choices":[{"id":1,"text":"Improve AI", "PollId":1},
{"id":2,"text":"Multiplayer","PollId":1},
{"id":3,"text":"Modern Art","PollId":1}]}}
That's my json response from the server. I need ember data RESTAdapter to parse it.
Upvotes: 0
Views: 212
Reputation: 348
You can use the EmberData EmbeddedRecordsMixin http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html and set choices
to be embedded.
Upvotes: 1
Reputation: 6396
I have to override extractSingle
method in DS.RESTSerializer
. See the emberjs link in the code for details.
// http://stackoverflow.com/questions/14320925/how-to-make-embedded-hasmany-relationships-work-with-ember-data
// http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_extractSingle
App.PollSerializer = DS.RESTSerializer.extend({
extractSingle: function(store, type, payload, id) {
var poll = payload.poll;
var choices = poll.choices;
delete poll.choices;
poll.choices = [];
choices.forEach(function(c) {
poll.choices.push(c.id);
});
payload = { choices: choices, poll: payload.poll };
return this._super(store, type, payload, id);
}
});
This turns my original structure into standard form.
Note: Ignorant fool that downvoted, is there a simpler way to do this, am i in some kind of pitfall, explain yourself.
Upvotes: 1