javascript - How to use two different adapters at the same time in Ember.js application? - Stack Overflow

I'm using basic adapter for one API:App.Store = DS.Store.extend({revision: 12,adapter: DS.BasicAda

I'm using basic adapter for one API:

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

Lets say I need to retrieve some data from an other service but using REST API:

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

How to use store2 then? Or is there another approach to solve issue like this?

I'm using basic adapter for one API:

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

Lets say I need to retrieve some data from an other service but using REST API:

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

How to use store2 then? Or is there another approach to solve issue like this?

Share Improve this question asked May 3, 2013 at 21:56 Wojciech BednarskiWojciech Bednarski 6,3939 gold badges50 silver badges75 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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

For Ember 2:

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

Source: DS.Adapter Class

When you need to use a different Store, define your Store and then specify the Model you want to retrieve:

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 it helps

This is how I made the above example work, note Im using ember-cli. Instead of creating my store with the DS.RESTAdapter.create(), or in my case, Im using DS.LSAdapter, I create my store in a initializer like this:

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

app.register('store:lsstore', app.LsStore);
app.register('adapter:-ls', DS.LSAdapter);

This basically registers a lsstore and a adapter:-ls on the container. Then I can inject my store into the application's route or controller, and this will try to find the adapter using adapter:-ls.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745101191a4611283.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信