In a route’s model hook, I send a request to the server for all Journal
models:
this.store.findAll('journal');
Thing is, Journal
is actually just an abstract class for anything “journal”. For example, I have a Report
and a Measurement
model both extending from Journal
. The server reponse (using REST) returns something like this:
{
"Journals": [
{ "id": "report_1", "type":"Report", ... },
{ "id": "measurement_1", "type":"Measurement", ... }
]
}
What I try to accomplish: a) Make sure they are normalized to the right models; b) Make sure findAll returns an array containing all these different types of records.
The first step is no problem. There are several hooks in either an adapter (handleResponse) or a serializer (normalize, normalizeFindAllResponse) to achieve this. Using the Ember Inspector I can see I have a Report
and Measurement
record pushed to the store instead of two Journal
records.
The second step is currently giving me a headache though. Whatever I try, the findAll always returns an empty array. Whatever hook I use to normalize to the right model, these models are somehow excluded from the findAll reponse by the “magic” of Ember Data.
So I created a custom findAll method on the journal
adapter which looks something like this:
findAll() {
const url = this.buildURL('journal', null, null, 'findAll');
return this.get('ajax').request(url).then(payload => {
return payload.map(journal => {
return this.get('store').push({
data: {
id: journal.id,
type: journal.type,
attributes: journal
}
});
});
});
}
Once again, the right records are pushed to the store, but once again, the route’s model hook receives an empty array. And once again, I’m wondering what Ember Data is doing “behind the scenes” to prevent me from returning an array containing all records. Additionally this also creates the next warnings:
WARNING: Encountered "0" in payload, but no model was found for model name "0" (resolved model name using care-home@serializer:journal:.modelNameFromPayloadKey("0"))
WARNING: Encountered "1" in payload, but no model was found for model name "1" (resolved model name using care-home@serializer:journal:.modelNameFromPayloadKey("1"))
Ofcourse I can ignore adapters/serializers and create a service with a method doing exactly what I want. But I’m trying to stick as close as possible to Ember Data since it’s a part of this project and this should be something I can accomplish using adapters/serializers.
Any help is greatly appreciated. Thanks.