1
votes

I'm a bit new to Ember, so please forgive me if this question illuminates my general lack of understanding of the framework.

Basically I am attempting to use Contentful with Ember. I found this addon, which seems to make this pretty simple https://github.com/davidpett/ember-data-contentful.

However, I am running into one issue that's beginning to really frustrate me. I have a content type, which has a "many reference" field, which allows a user to reference multiple of many different content types. Essentially, I have a "modules" field, which will be an array of several different modules. Each module will basically be it's own model.

In ember

modules: hasMany('module')

But module can be one of many different models, assuming there needs to be a model that corresponds to each content type.

I have already asked a question here: https://github.com/davidpett/ember-data-contentful/issues/24 but have been unable to get this working quite yet.

3

3 Answers

2
votes

To solve this you would need to have each of your models inherit from a base model that implements the Contentful system-metadata properties. They're found in the sys property in the json response.

You could then extend this base model depending on the content type of the base model to the specific model that would correspond to that content type.

I realize that you're trying to do something similar in the issue thread you posted. I think the problem you're running into is that you don't have a contentType property on your base class.

0
votes

Well, I have no idea of contentful.

My team did resolve this kind of dynamic types by using computed to request specific model manually. Since we didn't find a better way to make ember-data support multiple models in hasMany. And it works really well.
But for batch query, you may need to implement batch request collapse technique to improve the performance.

Another idea is, replace hasMany with DS.attr() as no type. And try whether you can pushPayload and peek those 'modules', and match your module models mannually to your model.modules in adapter. This might not work or only works for GET requests.
Or just match those payload model name/id to your model.modules, and use computed to peek the right record and mount to model.

0
votes

Turned out there was an issue with the ember-data-contentful lib. Downgrading to version 0.2.1 solves the problem for now. The author is aware of the bug and is looking into a fix in a future version. Thanks everyone for their help!