What is the expected JSON payload format for an array of model’s which each have hasMany
relationship.
I’ve been referencing this page for help: https://github.com/emberjs/data/blob/master/TRANSITION.md
Here are my models:
App.Result = DS.Model.extend({
time: DS.attr('string'),
duration: DS.attr('string'),
score: DS.attr('number'),
questionResults: DS.hasMany('questionResult')
});
App.QuestionResult = DS.Model.extend({
questionId: DS.attr('number'),
answerIndex: DS.attr('number'),
answerIndexChosen: DS.attr('number'),
duration: DS.attr('string'),
isCorrect: function () {
return (this.get('answerIndex') === this.get('answerIndexChosen'));
}.property("answerIndex", "answerIndexChosen")
});
I understand that if you are requesting a single model with something like: store.find('result', 123)
You would expect a response like:
{
"result": {
time: "00:01:02.03",
duration: "00:01:02.03",
score: 123,
questionResults: [1,2]
},
"questionResults": [
{
questionId: 1,
answerIndex: 1,
answerIndexChosen: 1,
duration: null
},
{
questionId: 2,
answerIndex: 2,
answerIndexChosen: 2,
duration: null
}
]
}
That was based on the post/comments example here: https://github.com/emberjs/data/blob/master/TRANSITION.md#rest-adapter-and-serializer-configuration
In my case I have a server returning an array of results with the QuestionResult
models embedded in the questionResults property of the Result model like [{…},{…}] and I know they should actually be ids like the following:
{
"results": [
{
time: "00:01:02.03",
duration: "00:01:02.03",
score: 123,
questionResults: [1,2]
},
{
time: "00:01:02.03",
duration: "00:01:02.03",
score: 123,
questionResults: [3,4]
}
]
}
but I don’t know where to put the embedded data.
I tried the following, but I am still getting errors when the serializer tries to find the embedded data.
{
"results": [
{
time: "00:01:02.03",
duration: "00:01:02.03",
score: 123,
questionResults: [1,2]
},
{
time: "00:01:02.03",
duration: "00:01:02.03",
score: 123,
questionResults: [3,4]
}
],
"questionResults": [
{
id: 1,
...
},
{
id: 2,
...
},
...
{
id: 4,
...
},
]
}