I have this interesting use case where I have two routes that return the same model. For example: /posts and /archived, both return an array of Post. I was hoping to implement the logic in my adapter such that I would switch the url based on the options I passed into the store, something like DS.Store.findAll('post', { archived: true }) then in my adapter I would have something like
DS.RESTAdapter.extend({
findAll(store, type, sinceToken, snapshotRecordArray, options) {
if(options.archived) { /* do something different */ }
}
})
Looking at https://github.com/emberjs/data/blob/master/addon/-private/system/store/finders.js#L140 I see that options are present, but not passed to the adapter. Is this a conscious design choice? If so, what is the reasoning?
Are there other options right now, besides creating a new model that inherits from Post?
Thoughts?
It looks like the snapshotRecordArray should have the options, as adapterOptionsproperty: see https://github.com/emberjs/data/blob/master/addon/-private/system/snapshot-record-array.js#L53
So you should be able to do:
DS.RESTAdapter.extend({
findAll(store, type, sinceToken, snapshotRecordArray) {
var options = snapshotRecordArray.adapterOptions;
if(options.archived) { /* do something different */ }
}
})
Please close this issue if this is working for you :)
That is exactly what I was looking for. Thanks!
@trevorrjohn be aware that you need to pass the options to the store.findAll like this:
this.store.findAll('user', {
adapterOptions: {
archived: true
}
});
This definitely needs to be documented better so thanks for opening this issue @trevorrjohn. I will open an issue for that.
Thanks, I did notice that. The only issue I am seeing now, is how Adapter.buildURL is defined, since the adapterOptions aren't passed through into that method, I have to override the full findAll method when really only the buildURL method needs to change. This is also making me think that this is not the best way to handle this scenario, thoughts?
:open_mouth: looks like the snaphot-array is not passed to buildURL within findAll.
Until that is fixed, you could use the following mega-hack as workaround:
import DS from "ember-data";
export default DS.JSONAPIAdapter.extend({
urlForFindAll: function(modelName, id, snapshot, requestType, query) {
// change to snapshot once it is passed correctly
if (this._snapshot.adapterOptions.archived) {
return "/archived";
}
return this._super(...arguments);
},
// remove this once snapshotArray is passed correctly to urlForFindAll
findAll(store, type, sinceToken, snapshotRecordArray) {
this._snapshot = snapshotRecordArray;
return this._super(...arguments);
}
});
@pangratz thanks. I will submit a PR for it, unless someone else is working on it.
@trevorrjohn go for it! Thanks!
looks like the snaphot-array is not passed to buildURL within findAll.
What I meant here, and failed to communicate clearly, is that the snapshotArray should be passed as the third argument: the third argument of buildURL is the snapshot/snapshotArray.
Most helpful comment
@pangratz thanks. I will submit a PR for it, unless someone else is working on it.