data
data copied to clipboard
polymorphic docs don't mention how to find/fetch polymorphic
Agreed -- they're also unclear on whether you have to do anything special with JSONAPI:
Note that, for polymorphism to work, Ember Data expects a "type" declaration polymorphic type via the reserved type property on the model. Confused?
I can't make head or tail of this sentence. Do I somehow have to mark types polymorphic with JSONAPI, or it enough that they just have a particular relationship?
Yeah that sentence is pretty awful :(
Roughly speaking, always give the most specific type possible to any resource your API returns.
For instance, if dog
extends pet
, return dog
not pet
. ember-data
would consider dog:1
and pet:1
to be separate entities if you were to return both, and any relationships would become weirdly broken.
My serializer needs the following code for my polymorphic relationship props
to work:
serialize(snapshot, options) {
let result = this._super(...arguments);
let props = snapshot.hasMany('props');
if(props) {
result.props = props.map(prop => ({
id: prop.id,
type: prop.modelName,
}));
}
return result;
},
Loading using pushPayload
does work automatically. I'm using the JSON serializer.
We're working on some big improvements to polymorphism that were unlocked by the relationship-refactor work and the identifiers work. The intent is to RFC an official polymorphism feature (polymorphism has mostly been undocumented because it never was fully designed) and we'll be sure to add a polymorphic guide at that time.
Until then, I'll see if we can get some small docs updates done to illuminate how the current setup works.
Explicit relationship polymorphism is now in place, polymorphic find was described by the identifiers RFC (though showing a basic config in the guides would be nice too). Polymorphic find is also covered by the RequestManager, since it is agnostic to type.
Broadly speaking, most things in EmberData are agnostic to type at this point, most polymorphic cases should "just work" excepting those in which you refer to the same resource by multiple identities, in which case you will need to configure the IdentifiersCache to be able to know that these identities refer to the same resource.
Closing in favor of the documentation quest in #8394