8

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 the users while avoiding sending an Ajax request for each user action.

Specifically, I would like to:

  1. Populate the LSAdapter with data from the server.
  2. All changes users make are updated to the LSAdapter. (I know how to do this step)
    (In preparation for #3, I can have a model that saves a log in LSAdapter of all the updates)
  3. Once in n minutes, the LSAdapter update data are sent back to the server.

My backend server is NOT Rails, so please make the answers generic.

Is it possible to use BOTH LSAdapter and RESTAdapter in an Ember app? If so, please provide code sample of how to set it up.
I would also appreciate if you provide code sample for steps 1 and 3, basically, how a database can talk to the local storage and vice versa.


If it's not possible to have both LSADapter and RESTAdapter, what can I do to accomplish steps 1 and 3?

The only get around I can think of is to set up the Ember app store as a RESTAdapter, but then call Web Storage localstorage directly in my app, not calling it from LSAdapter at all. Let me know if there is an easier or built-in way.

HaoQi Li
  • 11,970
  • 14
  • 58
  • 77

2 Answers2

2

After reading Ember data's DS.Store comments, it looks like it might be possible to use 2 adapters at once:

  1. You can retrieve models from the store in several ways. To retrieve a record for a specific id, use DS.Model's find() method:

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

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

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

I will update this answer if I try it out and get it working.


Update 1:

Check out the code in UPDATED for Additional question where both FixtureAdapter and RestAdapter are called.

Community
  • 1
  • 1
HaoQi Li
  • 11,970
  • 14
  • 58
  • 77
0
//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);
user10078
  • 681
  • 7
  • 7
  • as above question is applicable for my requirement as well. Still the question remains the same. The requirement is _Product_ model should act `RESTAdapter Model` while fetching data from the server and act `LocalStorageAdapter Model` while handling client side data management. – Sumit Ramteke Jun 04 '14 at 07:52