How to use two adapter(LS and Rest adapter) to a model, but route based access the adapter in ember.js application?

When I use the LS adapter to Foo model in first page, I defined/mentioned the DS.LSAdapter to that Foo model and get the model data from database to the local storage. after I can access/manage the model data using by local storage adapter in the first page. It is working fine here.

Next I will navigate to second page and this page model is defined/mentioned the Rest Adapter for Foo same model. Ember does not make REST API call here, because the problem is first LS adapter initiated in the first page and this second page is not initiate the REST adapter for that Foo model. How do we initiate the rest the adapter to Foo model in second page without page refresh?

Please you give the solution to fix it. Thank you so much.

Model

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

Mention the Local Storage Adapter in first page

Controller

App.FirstPageController = Ember.ObjectController.extend({

});

Route

App.FirstPageRoute = Ember.Route.extend({ 
      
    model: function() {  

        App.FooAdapter = DS.LSAdapter.extend({ namespace: 'ls-emberjs' });

        return this.store.find('foo');       

    }   

});

Hbs

<ul>
{{#each model}} 
    <li>fooID: {{id}} - Name: {{name}}</li>
{{/each}}
</ul>

Mention the Rest Adapter in second page

Controller

App.SecondPageController = Ember.ObjectController.extend({

});

Route

App.SecondPageRoute = Ember.Route.extend({  
     
    model: function() {  

        App.FooAdapter = DS.RESTAdapter.extend({});

        return this.store.find('foo');       

    }   

});

Hbs

<ul>
{{#each model}} 
    <li>fooID: {{id}} - Name: {{name}}</li>
{{/each}}
</ul>

Thanks

Could you give a little more info about what you are trying to do?

For instance, are you

- trying to have one page not refresh and one page that does? (find returns from store if it’s present, fetch will hit your server again).

- trying to have one page work offline and one page that does not? (there’s a few options here, but one would be to just use the Rest adapter, and then on the offline page check for the data in local storage yourself).

- trying to persist all retrieved data to local storage instead of in-memory? There isn’t a box solution to this yet (that I’m aware of) but I’d be willing to work on one with others (our cordova app seems to hit memory limits a little too often on lower quality Androids).

Hi jthoburn,

Thanks for reply my question.

my question is: How to use two adapter(LS and Rest adapter) to one model, but route based we access the adapter in ember.js application? Is it possible?

When I use LS adapter to foo model in first page, I defined/mentioned the DS.LSAdapter to that foo model and get the model data from database to the local storage. after I can access/manage the model data using by local storage adapter in the first page. It is working fine here.

Next I will navigate to second page and this page model is defined/mentioned the Rest Adapter for foo same model. Ember does not make REST API call here, because the problem is first LS adapter initiated in the first page and this second page is not initiate the REST adapter for that foo model. How do we resolve/initiate the rest the adapter to foo model in second page without page refres?

Please you give the solution to fix it. Thank you so much.

No, you cannot use two adapters for one model. Even if you did use hackish means to switch out the adapter for the model, you would be screwing with yourselves and your users trying to figure out all the buggy behavior that would cause.

What do I mean?

Well for starters, let’s say you used the RESTAdapter to download some data on page one, then switched to the LSAdapter when navigating to page two. Low and behold, there would be no data for the model. Why? Because the RESTAdapter does not store data into LocalStorage, it puts it in memory. The LSAdapter would have no data to find. The page using the LSAdapter and the page using the RESTAdapter would never be in sync.

You should ONLY be using one adapter for a model. If you are trying to use more you will inevitably end up with problems everywhere due to out-of-sync data.

The solution to fix this is to ask yourself why you are trying to use two adapters, and then instead of trying to use two adapters write code to address the underlying reason. If that reason is not one of the reasons listed in my original answer, ask again.

Thanks for you given more idea, I will use one adapter for a mode in my application.