How to use two different adapters simultaneously in an Ember.js application?

I use the base adapter for one API:

App.Store = DS.Store.extend({ revision: 12, adapter: DS.BasicAdapter.create() }); 

Suppose I need to get some data from another service, but using the REST API:

 App.Store2 = DS.Store.extend({ revision: 12, adapter: DS.RESTAdapter.create() }); 

How to use store2 then? Or is there a different approach to solving such a problem?

+4
source share
3 answers

When you need to use another store, define your store, and then specify the model that you want to get:

 App.Store = DS.Store.extend({ revision: 12, adapter: DS.BasicAdapter.create() }); App.store2 = DS.Store.create({ revision: 12, adapter: DS.RESTAdapter.create() }); // retrieving from custom store var myModelObject = App.store2.find(App.MyDifferentModel, 1); // retrieving from defaultStore is implicit for the Models defined var post = App.Post.find(1); 

hope this helps

+2
source

You can add two different adapters, no need to create several stores.

For Ember 2:

Model-specific adapters can be created by placing your adapter class in the app / adapters / + model-name + .js file of the application.

Source: DS.Adapter Class

+1
source

Here is how I did the above work example, note that I am using ember-cli . Instead of creating my repository with DS.RESTAdapter.create() or in my case Im using DS.LSAdapter , I create my store in the initializer as follows:

 app.LsStore = DS.Store.extend({ adapter: '-ls', }); app.register('store:lsstore', app.LsStore); app.register('adapter:-ls', DS.LSAdapter); 

Basically, this is the lsstore and adapter:-ls register on the container. Then I can enter my store in a route or controller application and this will try to find the adapter using adapter:-ls .

0
source

Source: https://habr.com/ru/post/1479070/


All Articles