Mapping Ember methods to API

53 views Asked by At

I'm using Ember Data with a Rails API and am wondering how I can map specific actions from Ember to the API.

For example, in looking at the Ember docs for deleteRecord and destroyRecord I see no mention of how these methods are translated to the backend. Is this configured in the RESTAdapter.

I'd like to know which Ember methods are already wired to send specific types of requests for basic CRUD actions (e.g. destroyRecord sends a DELETE request) but also how I could build or overwrite such a method from scratch--for example, say I wanted to create a remove method I could call on a DS.Model and have it map to /api/v1/users#destroy.

1

There are 1 answers

0
AudioBubble On BEST ANSWER

This is pretty basic and well-covered in the docs.

createRecord followed by a save generates a POST on /apinamespace/things.

save on an existing record generates a PUT on /apinamespace/things/thingID.

destroyRecord, or deleteRecord followed by a save, generates a DELETE on /apinamespace/things/thingID.

The Ember Data store and model methods themselves, by design, are unaware of the details of how they are implemented at the serializer and adapter level. They merely invoke appropriate routines from the serializer and adapter. So no, one would not expect the documentation for destroyRecord to talk about how it's implemented in a RESTAdapter/JSONSerializer case.

If you have a non-standard API scheme--let's say your DELETE method expects a URL of the form /apinamespace/things?id=thingID, for example--you can in some cases handle this by customizing the adapter. For instance you could override buildURL. For more complex situations, you may have to back off and do your own ajax/fetch call, after which you can push or pushPayload the results into the store if necessary.