DS.Adapter Class packages/ember-data/lib/system/adapter.js:20


Extends: Ember.Object

Defined in: packages/ember-data/lib/system/adapter.js:20

Module: ember-data

An adapter is an object that receives requests from a store and translates them into the appropriate action to take against your persistence layer. The persistence layer is usually an HTTP API, but may be anything, such as the browser's local storage.

Creating an Adapter

First, create a new subclass of DS.Adapter:

1
2
3
App.MyAdapter = DS.Adapter.extend({
  // ...your code here
});

To tell your store which adapter to use, set its adapter property:

1
2
3
App.store = DS.Store.create({
  adapter: App.MyAdapter.create()
});

DS.Adapter is an abstract base class that you should override in your application to customize it for your backend. The minimum set of methods that you should implement is:

  • find()
  • createRecord()
  • updateRecord()
  • deleteRecord()

To improve the network performance of your application, you can optimize your adapter by overriding these lower-level methods:

  • findMany()
  • createRecords()
  • updateRecords()
  • deleteRecords()
  • commit()

For an example implementation, see DS.RESTAdapter, the included REST adapter.

Show:

Methods

Show:

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.

Parameters:

store DS.Store
type subclass of DS.Model
the DS.Model class of the record
record DS.Model

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.

Parameters:

store DS.Store
type subclass of DS.Model
the DS.Model class of the record
record DS.Model

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;
}

Parameters:

store DS.Store
record DS.Model

serialize

(record, options)

Proxies to the serializer's serialize method.

Parameters:

record DS.Model
options Object

updateRecord

(store, type, record)

Implement this method in a subclass to handle the updating of a record.

Serializes the record update and send it to the server.

Parameters:

store DS.Store
type subclass of DS.Model
the DS.Model class of the record
record DS.Model