Creating a custom adapter method

What I want is to make a specific request to my backend and get back a list of models but it is not exactly a findAll/query call as i require an id (the url would be in the format v1/personType/{id}) so i need a separate url or adapter method.

So my question is: how would I make an adapter method such that the results are converted from plain json to models. I’ve tried making my own fetch request in an adapter method and then normalizing but normalizing didn’t do what i thought it would and its not being turned into models.

It seems strange to me that only findAll/query and a couple of other built in methods are the only ways that i can get ember to turn the results into models.

Any help is appreciated.

You’re not alone! Legacy Ember Data has some strange shapes you kinda have to force yourself into. If you have the stomach to adopt bleeding edge stuff (and are on a new enough version) you could look at using some of the new Ember Data concepts like the request manager. I haven’t actually used it myself yet so I can’t give you much guidance there but you could ask in Discord as well.

If you want to stick with legacy APIs for now I’d suggest just making the request however you’d like (fetch or whatever) and then use the store.push method to push the data into the store and return the models.

The example in the API docs is:

store.push(store.normalize('person', data));

You could define an adapter method to do this if you wanted, but it’s hard to say how much value that actually has. I guess whatever works for you code organization.

1 Like

Ah yes push works. I don’t know why but I thought push was similar to save and would make an api request so i never looked into it. Cheers!

1 Like