Can 2 different model use same adapter


#1

I have two model eg. twinfan and heavyblade… I want to use same adapter for both of them. Currently i am using two different adapter named twinfan.js and heavyblade.js. Can i create a same adapter for both the model and how?


#2

I think the simplest way to do this is to reexport.

// app/adapters/general-purpose.js
export default DS.Adapter.extend(...)
// app/adapters/twinfan.js
export { default } from './general-purpose';
// app/adapters/heavyblade.js
export { default } from './general-purpose';

#3

Thank you ef4 …just a thought can we specific something in model/twinfan.js and model/heavyblade.js So it directly use the adapter of general-purpose Which can save us creation of two file in adapter folder🤯


#4

Unfortunately not. Models don’t get to pick their adapter. It’s a feature I have wished for too. I think it’s because you don’t know exactly which model you’re dealing with until the adapter has handed you one.

There are other ways to do this, like customizing the resolver, but I think those are more confusing for the next person to read the code.


#5

Thank you @ef4 , for the detail explaination👍


#6

@ef4 @light44

Models don’t get to pick their adapter. It’s a feature I have wished for too. I think it’s because you don’t know exactly which model you’re dealing with until the adapter has handed you one.

This isn’t strictly true and the data team does not frown on extending the store and enhancing adapterFor and serializerFor to meet this use-case.

Generally you want an adapter or serializer per-api so making a mapping of models that use a specific api and using that for looking up the appropriate adapter and serializer is quite powerful. This is the mental model we will be making easier for folks to spot and use in the future for ember-data.