How to make the Ember Local Storage Adapter (LSAdapter) work with the REST ADB adapter?

I would like to take advantage of both the Ember Local Storage Adapter (LSAdapter) and the Ember REST Adapter (RESTAdapter), so that there is a centralized database for all users, avoiding sending an Ajax request for each user action.

In particular, I would like to:

  • Fill the LSAdapter with data from the server.
  • All changes made by users are updated in the LSAdapter. (I know how to take this step)
    (In preparation for # 3, I may have a model that saves the log in the LSAdapter of all updates).
  • After n minutes, the LSAdapter update data is sent back to the server.

My server server is NOT Rails, so please generalize the answers.

Can I use the BOTH LSAdapter and RESTAdapter in an Ember app? If yes, provide an example code on how to configure it.
I would also appreciate if you would provide a sample code for steps 1 and 3, mainly how the database can talk to local storage and vice versa.


If it is not possible to have both an LSADapter and a RESTAdapter, what can I do to complete steps 1 and 3?

The only thing I can think of is setting up the Ember application store as a RESTAdapter, and then calling Web Storage localstorage directly into my application, without calling it at all from LSAdapter. Let me know if there is a simpler or inline way.

+8
local-storage
source share
2 answers

After reading the comments of Ember DS.Store, it seems that it is possible to use 2 adapters at the same time :

  • You can get models from the store in several ways. To get an entry for a specific identifier, use the DS.Model find() method:

    var person = App.Person.find(123);

  • If your application has multiple instances of DS.Store (an unusual case), you can specify which storage should be used:

    var person = store.find(App.Person, 123);

I will update this answer if I try it and earn.


Update 1:

Check out the code in UPDATED for an additional question that calls both the Fixture adapter and the RestAdapter.

+2
source share
 //define your main adapter as usual App.Store = DS.Store.extend({ revision: 13, adapter: DS.RESTAdapter.create({ url: app.apiUrl, namespace: app.apiNamespace }) }); //App.Product for example will use the rest adapter App.Product = DS.Model.extend({ name: DS.attr('string'), }); App.Somemodel = DS.Model.extend({ name: DS.attr('string'), }); //App.Somemodel for example will use the fixture adapter App.Store.registerAdapter('App.Somemodel', DS.FixtureAdapter); 
0
source share

All Articles