I read this article and would like a little clarification and then have a question for handling a situation.
It appears the expected response for a GET or POST is something like this:
{
"post" : {
id: 1,
comments: [1,2,3]
},
"comments" : [
{
id: 1,
comment: "Bob Loblaw's Law Blog"
}, {...}, {...}
]
}
Or, since Ember-data supports a polymorphic model property, here is another possibility:
{
"event" : {
id: 1,
mainContact: {
id: 23,
type: "contact" // makes it polymorphic
}
}
}
So that tells Ember-data that “mainContact” is an instance of “contact”. It creates an empty App.Contact and awaits a request for data off of it before fetching it from the server.
So what about this situation? Consider being handed a URL and navigating to a page that is loading the aforementioned event. Since it’s the first time the system will be loading the event, it will ultimately need all the data associated with it, including all the details about the mainContact (so it doesn’t have to make another request to retrieve that data–though it could and that is what Ember-data seems to expect).
So the response might look like this:
{
"event" : {
id: 1,
mainContact: {
id: 23,
firstName: "Kevin",
lastName: "Beez",
type: "contact" // makes it polymorphic
}
}
}
…or maybe, more properly, like this (with the extra data side-loaded):
{
"event" : {
id: 1,
mainContact: {
id: 23,
type: "contact" // makes it polymorphic
}
},
"contacts" : [
{
id: 23,
firstName: "Kevin",
lastName: "Beez"
}
]
}
Can Ember-data handle this? Is this a valid thought process?
Here’s the research I’ve done so far While debugging, trying to figure out how this could be done, I determined a couple places this could be accomplished.
-
Using the first method (including the embedded information) and within deserializeRecordId, if the object is polymorphic, add an if-check to say, “if record.isEmpty, here’s the data for it”. I would think that declaring a model with “embedded: always” would make this method believe the parameter has all the data available to create a populated record. That might look like this:
} else if (typeof id === "object") { // polymorphic var record = store.recordForId(id.type, id.id); if (record.isEmpty) { // or if relationship.options.embedded === "always" record.setupData(id); } data[key] = record; }
-
Using the second method seems like it should just work, inherently. Ember-data seems to handle side-loaded data pretty effectively. And it may, I might just be doing it incorrectly. Taking another example, I would think this should work, but it does not:
{ "event" : { id: 1, contact: 23 }, "contact" : { id: 23, firstName: "Kevin", lastName: "Beez" } }
Thanks for reading my book!