What is the expected JSON format for an array of models which each have hasMany relationships


#1

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,
			...
		},
	]
}

#2

Uh, disregard this topic. I’m not sure how to delete it. Anyways, I think the above format is correct, but I think I had some other unrelated problems causing the issue. Possibly a circular reference problem going in my override of the serializer.normalize(type,hash) …


#3

@matt​mazzola the guides and api docs are starting to fill in well, perhaps look at the source of the guides in the website project for the latest docs : https://github.com/emberjs/website/tree/master/source/guides/models


#4

I would agree there has been a lot of improvement, although I feel the docs are currently on a scary threshold. There is just enough documentation to convince you that you could create something, and until you get stuck everything feels great; however once you do get stuck stuck there is no other option besides reading ember-data source code to figure out what’s happening and that task becomes so overwhelming most people will quit.

For instance, I the most important page for my project would be filtering records and as you can see:

That page is empty… :frowning:

Anyways, I realize people are working as hard and as fast as they can and I thank them for that.

Since this post didn’t die. We might as well make it helpful for future viewers. I think the link you posted is helpful and I would also list: http://jsonapi.org/

I’m not sure if this is true, but from what I remember about an old blog post ember-data expects formats that loosely match the jsonapi spec so it might point you in the right direction or help make sense of the default code you see in the extract* normalize* functions of your serializers.