Ember change normalizeResponse based on queried model

Multi tool use
Ember change normalizeResponse based on queried model
I'm using a second datastore with my Ember app, so I can communicate with a separate external API. I have no control over this API.
With a DS.JSONSerializer
I can add some missing properties like id
:
DS.JSONSerializer
id
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
if (requestType == 'query') {
payload.forEach(function(el, index) {
payload[index].id = index
})
}
Now I can do some different tricks for each different requestType
. But every response is parsed. Now sometimes a response from one request needs to be parsed differently.
requestType
So what I am trying to do is change the normalizeResponse
functionality for each different request path (mapped to a fake model using pathForType
in an adapter for this store). But the argument store
is always the same (obviously) and the argument promaryModelClass
is always "unknown mixin" - not sure if this can be any help.
normalizeResponse
pathForType
store
promaryModelClass
How can I find what model was requested? With this information I could do a switch()
in normalizeResponse
.
switch()
normalizeResponse
Is there a different way to achieve my goal that does not require me to make a separate adapter for every path/model?
There are over a dozen normalize functions available. Something should work for what I am trying to achieve.
Good advise, although I knew this already. The method is very verbose. I'm specifically looking for a way to do this in my one adapter or serializer that works fine for everything else.
– Redsandro
Jul 1 at 15:17
Docs say that
primaryModelClass
is a DS.Model
which means primaryModelClass.modelName
should return model name. And I just tested it on my project and it contains model name.– Gennady Dogaev
Jul 1 at 15:29
primaryModelClass
DS.Model
primaryModelClass.modelName
@GennadyDogaev this works! This is exactly what I am looking for. I tried
primartModelClass.name
which is always Class
. You put two and two together. Turn this into an answer and I will accept it.– Redsandro
Jul 1 at 20:07
primartModelClass.name
Class
2 Answers
2
You can use primaryModelClass.modelName
.
primaryModelClass.modelName
I think this is a great example of a use case of not using ember data.
Assuming that you have models A,B,C that are all working great with ember data, leave those alone.
I'd create a separate service and make raw requests to that different endpoint. So you'd replace this.store.query('thing', {args})
with a separate service that uses ember-ajax (or ember-fetch or whatever). If you need, you can use that service to hold the data that you need (Ember-data is just a service anyway) or you can create models and push them into the store manually.
this.store.query('thing', {args})
Without knowing more about your exact situation, hard to give a specific code/advice, but I'd just avoid this problem and write your own custom service.
I am actually using a
service
/serializer
/adapter
combo to implement a custom "datastore", separate from the default data store. I'm specifically looking for a way to normalize every response, depending on what the actual request path/model was. As @GennadyDogaev pointed out, this can be done with a separate adapter for every model, but I'm looking for a way to do this using normalization in a single file. There are over a dozen normalization functions available. Something should work, right?– Redsandro
Jul 1 at 15:19
service
serializer
adapter
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You can create separate serializer/adapter for model (name it the same as model and ember will use it for that model).
– Gennady Dogaev
Jun 28 at 16:27