What is the difference between adapter and Fixture adapter and REST adapter in ember-data?

What is the difference between adapter and adapter adapter and REST adapter and when to use them?

+7
source share
1 answer

Use DS.FixtureAdapter (or DS.FixtureAdapter.create() ) when you are not executing (yet?) To keep in touch with the backend, but save your data as β€œlights” in the client. Once you have declared the model:

 App.Thing = DS.Model.extend({ name: DS.attr('string'), // ... }); 

You can define your lights:

 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 until the page (i.e. javascript environment).

DS.RestAdapter , although apparently still under development, was designed to work well with the Rails API, but can probably be modified / extended to work with any RESTful API with which you work. He knows how to handle App.Thing.findAll() by calling /things , and handle App.Thing.find(12) when calling /things/12 . This is the relative path added to the namespace parameter that you pass in:

 App.store = DS.Store.create({ revision: 4, adapter: DS.RestAdapter.create({ namespace: 'http://what.ever/api/v1' }) }); 

DS.Adapter is pretty abstract: the superclass of the above built-in adapters. If you do not satisfy your needs, you may want to realize 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 this helps. See readme doc https://github.com/emberjs/data for more information.

+22
source

All Articles