DS.ActiveModelAdapter Class packages/activemodel-adapter/lib/system/active_model_adapter.js:11
Extends: DS.Adapter
Defined in: packages/activemodel-adapter/lib/system/active_model_adapter.js:11
Module: ember-data
The ActiveModelAdapter is a subclass of the RESTAdapter designed to integrate with a JSON API that uses an underscored naming convention instead of camelcasing. It has been designed to work out of the box with the activemodelserializers Ruby gem.
This adapter extends the DS.RESTAdapter by making consistent use of the camelization, decamelization and pluralization methods to normalize the serialized JSON into a format that is compatible with a conventional Rails backend and Ember Data.
JSON Structure
The ActiveModelAdapter expects the JSON returned from your server to follow the REST adapter conventions substituting underscored keys for camelcased ones.
Conventional Names
Attribute names in your JSON payload should be the underscored versions of the attributes in your Ember.js models.
For example, if you have a Person
model:
1 2 3 4 5 |
App.FamousPerson = 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 |
{ "famous_person": { "first_name": "Barack", "last_name": "Obama", "occupation": "President" } } |
Methods
ajaxError
(jqXHR)
The ActiveModelAdapter overrides the ajaxError
method
to return a DS.InvalidError for all 422 Unprocessable Entity
responses.
A 422 HTTP response from the server generally implies that the request was well formed but the API was unable to process it because the content was not semantically correct or meaningful per the API.
For more information on 422 HTTP Error code see 11.2 WebDAV RFC 4918 https://tools.ietf.org/html/rfc4918#section-11.2
Parameters:
- jqXHR
Returns:
- error
createRecord
(store, type, record)
Implement this method in a subclass to handle the creation of new records.
Serializes the record and send it to the server.
This implementation should call the adapter's didCreateRecord
method on success or didError
method on failure.
deleteRecord
(store, type, record)
Implement this method in a subclass to handle the deletion of a record.
Sends a delete request for the record to the server.
find
The find()
method is invoked when the store is asked for a record that
has not previously been loaded. In response to find()
being called, you
should query your persistence layer for a record with the given ID. Once
found, you can asynchronously call the store's push()
method to push
the record into the store.
Here is an example find
implementation:
1 2 3 4 5 6 7 8 9 10 11 |
find: function(store, type, id) { var url = type.url; url = url.fmt(id); jQuery.getJSON(url, function(data) { // data is a hash of key/value pairs. If your server returns a // root, simply do something like: // store.push(type, id, data.person) store.push(type, id, data); }); } |
findAll
(store, type, since)
private
Parameters:
- store
- type
- since
findMany
(store, type, ids)
Find multiple records at once.
By default, it loops over the provided ids and calls find
on each.
May be overwritten to improve performance and reduce the number of
server requests.
Parameters:
- store DS.Store
- type subclass of DS.Model
- the DS.Model class of the records
- ids Array
findQuery
(store, type, query, recordArray)
private
Parameters:
- store
- type
- query
- recordArray
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)
The ActiveModelAdapter overrides the pathForType
method to build
underscored URLs by decamelizing and pluralizing the object type name.
1 2 |
this.pathForType("famousPerson"); //=> "famous_people" |
Parameters:
- type String
Returns:
- String
serialize
(record, options)
Proxies to the serializer's serialize
method.
Parameters:
- record DS.Model
- options Object