What is the difference between Adapter and Fixture Adapter and REST Adapter, and when to use each one?
In Ember Data, an Adapter determines how data is persisted to a backend data store. Things such as the backend host, URL format and headers used to talk to a REST API can all be configured in an adapter. Ember Data's default Adapter has some built-in assumptions about how a REST API should look.
REST adapter is a solution where ITX can readily call any Web/REST API and provide easy mapping to and from the APIs. REST Adapter is a kind of framework which addresses multiple key features: Integration of different applications involving REST APIs rather than custom programming APIs.
In Ember Data, models are objects that represent the underlying data that your application presents to the user. Note that Ember Data models are a different concept than the model method on Routes, although they share the same name.
Use DS.FixtureAdapter
(or DS.FixtureAdapter.create()
) when you don't (yet?) care to communicate with a backend, but will store your data as "fixtures" in the client. Once you've declared a model:
App.Thing = DS.Model.extend({
name: DS.attr('string'),
// ...
});
You can define your fixtures:
App.Thing.FIXTURES = [
{
id: 1,
name: '...',
// ...
},
{
id: 2,
name: '...',
// ...
},
];
And then you can use the ember-data methods on them (e.g. App.Thing.findAll()
, etc.) and manipulate them, but of course it will only persist as long as the page does (i.e. the javascript environment).
DS.RestAdapter
, usable though apparently still under development, was designed to fit well with a Rails API, but could probably be modified / extended to work with whatever RESTful API you're working with. It knows to process App.Thing.findAll()
by making a call to /things
, and to process App.Thing.find(12)
with a call to /things/12
. This is a relative path, appended to the namespace parameter you pass in:
App.store = DS.Store.create({
revision: 4,
adapter: DS.RestAdapter.create({
namespace: 'http://what.ever/api/v1'
})
});
DS.Adapter
is rather abstract: the superclass of the aforementioned built-in Adapters. If neither suit your needs, you may well want to implement your own:
App.adapter = DS.Adapter.create({
find: function(store, type, id) {
// ...
jQuery.get( ... , function(data) {
store.load(type, id, data);
});
},
createRecord: function(store, type, model) {
// ...
jQuery.post( ... , function(data) {
store.didCreateRecord(model, data);
});
},
// ...
});
App.store = DS.Store.create({
revision: 4,
adapter: App.adapter
});
Hope that helps. See the readme doc at https://github.com/emberjs/data for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With