Ember-data-offline is addon that provides some mixin that allows to achieve to main goals - to backup data, received from backend API to local storage to allow app to work in offline mode and to use this storage as a cache.
ember install ember-data-offline
First you need to define your application adapter:
import baseAdapter from 'ember-data-offline/adapters/base';
export default baseAdapter.extend({
offlineNamespace: 'foo'//optional
});
Then specify model and define serializer for it (for now it is required, later we'll fix it):
import DS from 'ember-data';
export default DS.RESTSerializer.extend({
});
And basically, that's all!
If you have 'id'
in your model different from default ('_id'
for example), you need to specify it in adapter for the model like that:
export default appAdapter.extend({
serializerPrimaryKey: '_id',
});
and also in serializer for that model you need to use mixin for translating it to normal 'id'
:
import DS from 'ember-data';
import offlineIdMixin from 'ember-data-offline/mixins/localstorage-id'; //thing that you need!
export default DS.RESTSerializer.extend(offlineIdMixin, {
primaryKey: '_id', //usual ember-data stuff
});
This addon contains of two essential parts. First - queue
and jobs
, second - online
and offline mixins
.
Queue
is just super easy task runner and used to make kind of background proccessing mechanism and basically we use it to synchronize data flows from online api and local storage.
Online mixin
redefine all adapter methods for finding, creation, deletion to to make request to online api, but with running offline job to make offline data actual.
Same for offline mixin
, but vice versa.
As you see it is as simple as possible and names of mixins say mostly all about them :-).
Example of usage of these mixins you can see in addon/adapters/base.js
.
Also we created this base adapter (addon/adapters/base.js
) to simplify setup proccess. By default it use RESTAdapter
and LocalForageAdapter
, but you can use your own adapters that you like.
- fork repo
git clone [email protected]:your-github/ember-data-offline.git
npm i && bower install
- add your feature
- cover with tests
- send PR!
[Licensed under MIT license] 1