DS.RESTAdapter Class packages/ember-data/lib/adapters/rest_adapter.js:13
Extends: DS.Adapter
Defined in: packages/ember-data/lib/adapters/rest_adapter.js:13
Module: ember-data
The REST adapter allows your store to communicate with an HTTP server by transmitting JSON via XHR. Most Ember.js apps that consume a JSON API should use the REST adapter.
This adapter is designed around the idea that the JSON exchanged with the server should be conventional.
JSON Structure
The REST adapter expects the JSON returned from your server to follow these conventions.
Object Root
The JSON payload should be an object that contains the record inside a
root property. For example, in response to a GET
request for
/posts/1
, the JSON should look like this:
1 2 3 4 5 6 |
{ "post": { title: "I'm Running to Reform the W3C's Tag", author: "Yehuda Katz" } } |
Conventional Names
Attribute names in your JSON payload should be the camelCased versions of the attributes in your Ember.js models.
For example, if you have a Person
model:
1 2 3 4 5 |
App.Person = DS.Model.extend({ firstName: DS.attr('string'), lastName: DS.attr('string'), occupation: DS.attr('string') }); |
The JSON returned should look like this:
1 2 3 4 5 6 7 |
{ "person": { "firstName": "Barack", "lastName": "Obama", "occupation": "President" } } |
Customization
Endpoint path customization
Endpoint paths can be prefixed with a namespace
by setting the namespace
property on the adapter:
1 2 3 |
DS.RESTAdapter.reopen({ namespace: 'api/1' }); |
Requests for App.Person
would now target /api/1/people/1
.
Host customization
An adapter can target other hosts by setting the host
property.
1 2 3 |
DS.RESTAdapter.reopen({ host: 'https://api.example.com' }); |
Headers customization
Some APIs require HTTP headers, e.g. to provide an API key. An array of headers can be added to the adapter which are passed with every request:
1 2 3 4 5 6 |
DS.RESTAdapter.reopen({ headers: { "API_KEY": "secret key", "ANOTHER_HEADER": "Some header value" } }); |
Methods
ajax
(url, type, hash)
private
Takes a URL, an HTTP method and a hash of data, and makes an HTTP request.
When the server responds with a payload, Ember Data will call into extractSingle
or extractArray
(depending on whether the original query was for one record or
many records).
By default, ajax
method has the following behavior:
- It sets the response
dataType
to"json"
- If the HTTP method is not
"GET"
, it sets theContent-Type
to beapplication/json; charset=utf-8
- If the HTTP method is not
"GET"
, it stringifies the data passed in. The data is the serialized record in the case of a save. - Registers success and failure handlers.
Parameters:
- url
- type
- hash
ajaxError
(jqXHR)
Takes an ajax response, and returns a relavant error.
By default, the ajaxError
method has the following behavior:
- It simply returns the ajax response (jqXHR).
Parameters:
- jqXHR
buildURL
(type, id)
Builds a URL for a given type and optional ID.
By default, it pluralizes the type's name (for example, 'post' becomes 'posts' and 'person' becomes 'people').
If an ID is specified, it adds the ID to the path generated
for the type, separated by a /
.
Returns:
- String
createRecord
(store, type, record)
Called by the store when a newly created record is
saved via the save
method on a model record instance.
The createRecord
method serializes the record and makes an Ajax (HTTP POST) request
to a URL computed by buildURL
.
See serialize
for information on how to customize the serialized form
of a record.
Returns:
- Promise
deleteRecord
(store, type, record)
Called by the store when a record is deleted.
The deleteRecord
method makes an Ajax (HTTP DELETE) request to a URL computed by buildURL
.
Returns:
- Promise
find
(store, type, id)
Called by the store in order to fetch the JSON for a given type and ID.
The find
method makes an Ajax request to a URL computed by buildURL
, and returns a
promise for the resulting payload.
This method performs an HTTP GET
request with the id provided as part of the querystring.
Returns:
- Promise
findAll
(store, type, sinceToken)
private
Called by the store in order to fetch a JSON array for all of the records for a given type.
The findAll
method makes an Ajax (HTTP GET) request to a URL computed by buildURL
, and returns a
promise for the resulting payload.
Returns:
- Promise
findBelongsTo
(store, record, url)
Called by the store in order to fetch a JSON array for
the unloaded records in a belongs-to relationship that were originally
specified as a URL (inside of links
).
For example, if your original payload looks like this:
1 2 3 4 5 6 7 |
{ "person": { "id": 1, "name": "Tom Dale", "links": { "group": "/people/1/group" } } } |
This method will be called with the parent record and /people/1/group
.
The findBelongsTo
method will make an Ajax (HTTP GET) request to the originally specified URL.
Returns:
- Promise
findHasMany
(store, record, url)
Called by the store in order to fetch a JSON array for
the unloaded records in a has-many relationship that were originally
specified as a URL (inside of links
).
For example, if your original payload looks like this:
1 2 3 4 5 6 7 |
{ "post": { "id": 1, "title": "Rails is omakase", "links": { "comments": "/posts/1/comments" } } } |
This method will be called with the parent record and /posts/1/comments
.
The findHasMany
method will make an Ajax (HTTP GET) request to the originally specified URL.
If the URL is host-relative (starting with a single slash), the
request will use the host specified on the adapter (if any).
Returns:
- Promise
findMany
(store, type, ids)
Called by the store in order to fetch a JSON array for the unloaded records in a has-many relationship that were originally specified as IDs.
For example, if the original payload looks like:
1 2 3 4 5 |
{ "id": 1, "title": "Rails is omakase", "comments": [ 1, 2, 3 ] } |
The IDs will be passed as a URL-encoded Array of IDs, in this form:
1 |
ids[]=1&ids[]=2&ids[]=3 |
Many servers, such as Rails and PHP, will automatically convert this URL-encoded array into an Array for you on the server-side. If you want to encode the IDs, differently, just override this (one-line) method.
The findMany
method makes an Ajax (HTTP GET) request to a URL computed by buildURL
, and returns a
promise for the resulting payload.
Parameters:
- store DS.Store
- type subclass of DS.Model
- ids Array
Returns:
- Promise
findQuery
(store, type, query)
private
Called by the store in order to fetch a JSON array for the records that match a particular query.
The findQuery
method makes an Ajax (HTTP GET) request to a URL computed by buildURL
, and returns a
promise for the resulting payload.
The query
argument is a simple JavaScript object that will be passed directly
to the server as parameters.
Parameters:
- store DS.Store
- type subclass of DS.Model
- query Object
Returns:
- Promise
generateIdForRecord
(store, record)
If the globally unique IDs for your records should be generated on the client,
implement the generateIdForRecord()
method. This method will be invoked
each time you create a new record, and the value returned from it will be
assigned to the record's primaryKey
.
Most traditional REST-like HTTP APIs will not use this method. Instead, the ID
of the record will be set by the server, and your adapter will update the store
with the new ID when it calls didCreateRecord()
. Only implement this method if
you intend to generate record IDs on the client-side.
The generateIdForRecord()
method will be invoked with the requesting store as
the first parameter and the newly created record as the second parameter:
1 2 3 4 |
generateIdForRecord: function(store, record) { var uuid = App.generateUUIDWithStatisticallyLowOddsOfCollision(); return uuid; } |
pathForType
(type)
Determines the pathname for a given type.
By default, it pluralizes the type's name (for example, 'post' becomes 'posts' and 'person' becomes 'people').
Pathname customization
For example if you have an object LineItem with an endpoint of "/line_items/".
1 2 3 4 5 6 |
DS.RESTAdapter.reopen({ pathForType: function(type) { var decamelized = Ember.String.decamelize(type); return Ember.String.pluralize(decamelized); }; }); |
Parameters:
- type String
Returns:
- String
serialize
(record, options)
Proxies to the serializer's serialize
method.
Parameters:
- record DS.Model
- options Object
updateRecord
(store, type, record)
Called by the store when an existing record is saved
via the save
method on a model record instance.
The updateRecord
method serializes the record and makes an Ajax (HTTP PUT) request
to a URL computed by buildURL
.
See serialize
for information on how to customize the serialized form
of a record.
Returns:
- Promise