ember-data-contentful
ember-data-contentful copied to clipboard
Handling polymorphic references for records
- [x] Build test data
- [x] Identify the problem
- [ ] Fix it
This addresses issue #24
So, I think I know where I went wrong. I made the assumption that you would have a model for every content type inside of Contentful. In that assumption, I also assumed that the name of your model would be pulled from the content_type
when it appears that you'd rather have the model name you give it inside the hasMany
reference. (e.g. section from the test data above.)
Does that sound correct @bretmorris?
/cc @davidpett
@morganick i just pushed a failing polymorphic demo to https://github.com/davidpett/ember-data-contentful/tree/failing-polymorphic if you ember s
and visit http://localhost:4200/test-page and look at the ember inspector, you will see 1 module
all with undefined values and 1 'module-contentwith actual values. logging the contentType of each module renders
undefined`
{{#each model.modules as |module|}}
{{log module.contentType}}
{{/each}}
@davidpett Thanks for doing that. I'll take a look.
I finally got some time to dive into this and try to find a solution to this issue. Here are a couple of things that I have noticed; let me know if I'm missing something.
When we are parsing the relationships for the page
model, we pass in the relationship name of module
. So at that point I have a type
of module
and an id
of the entity inside Contentful. This is, currently, all I have for the resource linkage. (http://jsonapi.org/format/#document-resource-object-linkage) That's all fine and good until we parse the includes which is where the related models are loaded. (e.g. module-content
in the case of the dummy app)
When we parse the includes, we use the contentType
from Contentful to tell ember-data
which model it is and push it into the store. With a non-polymorphic hasMany
this works as intended. This is how module
model gets initialized but there is no data, and model-content
gets data but is not linked to the page
via the hasMany
.
I've also looked through the 0.2.1 code to see what the differences where and where I broke this since it appears to work in 0.2.1. So far it has eluded me. My next step here will be to pull in the 0.2.1 serializer up to where we have the test suite and see if my tests pass there.
The other thing I was looking at doing was supporting true polymorphic hasMany
for this. This used to be something that ember-data
handled out of the box but seems to have been removed when they switched over to JSONAPI. I've read a couple of closed PRs with grumblings towards that.
Also, if we don't support polymorphic then you lose the ability to do findRecord
and queryRecord
on the related content types. module-content
is what you would use for those not the generic module
which has no meaning outside of the page
model in Contentful.
@davidpett & @bretmorris just wanted to give you an update on the status of this issue. Let me know if you have other ideas or suggestions.
@morganick @davidpett Were y'all able to get any further with this issue? I'm in the same boat of needing polymorphic references.