Customizing Adapters

In Ember Data, the Adapter determines how data is persisted to a
backend data store, such as the URL format and headers for a REST API.
(The format of the data itself is determined by the
serializer.)
Ember Data's default Adapter has some built-in
assumptions of how a REST API should look. If
your backend conventions differ from these assumptions Ember Data
makes it easy to change its functionality by swapping out or extending
the default Adapter.

Some reasons for customizing an Adapter include using
underscores_case in your urls, using a medium other than REST to
communicate with your backend API or even using a
local storage backend.

Extending Adapters is a natural process in Ember Data. Ember takes the
position that you should extend an adapter to add different
functionality. This results in code that is
more testable, easier to understand and reduces bloat for people who
may want to subclass your adapter.

If your backend has some consistent rules you can define an
adapter:application. The adapter:application will get priority over
the default Adapter, however it will still be superseded by model
specific Adapters.

If you have one model that has exceptional rules for communicating
with its backend than the others you can create a Model specific
Adapter by running the command ember generate adapter adapter-name.
For example, running ember generate adapter post will create the
following file:

Ember Data comes with several built-in adapters.
Feel free to use these adapters as a starting point for creating your own custom adapter.

DS.Adapter is the basic adapter
with no functionality. It is generally a good starting point if you
want to create an adapter that is radically different from the other
Ember adapters.

DS.JSONAPIAdapter
The JSONAPIAdapter is the default adapter and follows JSON API
conventions to communicate with an HTTP server by transmitting JSON
via XHR.

DS.RESTAdapter
The RESTAdapter allows your store to communicate with an HTTP server
by transmitting JSON via XHR. Before Ember Data 2.0 this adapter was the default.

Customizing the JSONAPIAdapter

The
DS.JSONAPIAdapter
has a handful of hooks that are commonly used to extend it to work
with non-standard backends.

URL Conventions

The JSONAPIAdapter is smart enough to determine the URLs it
communicates with based on the name of the model. For example, if you
ask for a Post by ID:

store.findRecord('post', 1).then(function(post) {
});

The JSON API adapter will automatically send a GET request to /posts/1.

The actions you can take on a record map onto the following URLs in the
JSON API adapter:

Action

HTTP Verb

URL

Find

GET

/posts/123

Find All

GET

/posts

Update

PATCH

/posts/123

Create

POST

/posts

Delete

DELETE

/posts/123

Pluralization Customization

To facilitate pluralizing model names when generating route urls Ember
Data comes bundled with
Ember Inflector, an
ActiveSupport::Inflector compatible library for inflecting words
between plural and singular forms. Irregular or uncountable
pluralizations can be specified via Ember.Inflector.inflector.
A common way to do this is:

This will tell the JSON API adapter that requests for formula
should go to /formulae/1 instead of /formulas/1, and that
requests for advice should go to /advice/1 instead of /advices/1.

When specifying irregular inflection rules for compound words, only the final word or phrase should be specified. For example, to specify the plural of redCow as redKine or red-cow as red-kine, only the final word segments cow and kine should be specified:

inflector.irregular('cow', 'kine');

Endpoint Path Customization

The namespace property can be used to prefix requests with a
specific url namespace.

Requests for person would now target https://api.example.com/people/1.

Path Customization

By default, the JSONAPIAdapter will attempt to pluralize and dasherize
the model name to generate the path name. If this convention does not
conform to your backend you can override the pathForType method.

For example, if you did not want to pluralize model names and needed
underscore_case instead of camelCase you could override the
pathForType method like this:

Requests for person would now target /person/1.
Requests for user-profile would now target /user_profile/1.

Headers customization

Some APIs require HTTP headers, e.g. to provide an API key. Arbitrary
headers can be set as key/value pairs on the JSONAPIAdapter's headers
object and Ember Data will send them along with each ajax request.

In some cases, your dynamic headers may require data from some
object outside of Ember's observer system (for example
document.cookie). You can use the
volatile
function to set the property into a non-cached mode causing the headers to
be recomputed with every request.

Authoring Adapters

The defaultSerializer property can be used to specify the serializer
that will be used by this adapter. This is only used when a model
specific serializer or serializer:application are not defined.

In an application, it is often easier to specify an
serializer:application. However, if you are the author of a
community adapter it is important to remember to set this property to
ensure Ember does the right thing in the case a user of your adapter
does not specify an serializer:application.