Hi all,
I’ve been writing a serializer for my server component to serialize to / deserialize from JSONAPI documents and I want to incorporate the “?include” query parameter for inclusion of related resources.
What I’ve done to get this to work so far is this:
this.store.findRecord('first', 1, {
adapterOptions: {
include: ['seconds', 'seconds.thirds']
}
});
And extended the JSONAPIAdapter
like so:
buildURL(modelName, id, snapshot, requestType, query) {
let url = this._super(...arguments);
let include = Ember.get(snapshot, 'adapterOptions.include') || [];
if (include.length === 0) {
return url;
}
return url + '?include=' + include.join(',');
},
So firstly, adapterOptions
is the only way I’ve found so far to get arguments from the findRecord
method to the adapter so that it may append the includes to the URL. Does anyone know if this is the correct approach to take? When a snapshot is created, this property is set on it.
Secondly, if this is the correct approach, I’ve also had to override the findAll
method on the JSONAPIAdapter
to include the a 4th argument snapshotArray
which is set by _findAll
in “finder.js”. This is then passed into the buildURL method to access the adapterOptions
(the current implementation passes null
into the snapshot parameter).
findAll(store, type, sinceToken, snapshotArray) {
let query;
if (sinceToken) {
query = { sinceToken };
}
let url = this.buildURL(type.modelName, null, snapshotArray, 'findAll');
return this.ajax(url, 'GET', { data: query });
}