简体   繁体   中英

Using DS.FixtureAdapter and DS.RESTAdapter at the same time

I would like to use both adapters, depending on the route. Currently I have the following store:

if (window.USE_FIXTURES) { var my_adapter = 'DS.FixtureAdapter'; }
else                     { var my_adapter = SettingsApp.Adapter.create(); }

SettingsApp.Store = DS.Store.extend({
    revision: 12,
    adapter:  my_adapter
});

( SettinsApp.Adapter is a DS.RESTAdapter )

But what I would like is to have two stores. Is it possible to do this, on a route-by-route basis? Could somebody provide an example on how to configure the router for this?

But what I would like is to have two stores. Is it possible to do this, on a route-by-route basis?

As per design you should never want to have multiple stores in your App. But instead the possibility to have an adapter on a per model basis exists.

This would look something like this:

DS.Store.registerAdapter('App.LegacyModel', App.MyLegacyAdapter.extend({});

If you have multiple models that use the same adapter you could do:

DS.Store.registerAdapter(['App.FooModel', 'App.BarModel'], App.MyAdapter.extend({});

The store will then use the model type corresponding adapter when making requests.

You can still specify a default adapter for the store by using the this syntax:

App.Store = DS.Store.extend({
  adapter: 'DS.RESTAdapter'
});

This default adapter will be used as a fallback if you try to load a record that does not have an adapter specified for its type.

Is it possible to do this, on a route-by-route basis?

Following best practices, if you want to use a different model other than the one looked up by convention (eg using your route's name) you could do something like:

App.SomeRoute = Ember.Route.extend({
  setupController: function(controller, model) {
    controller.set('model', App.MySpecialModel.find());
  }
});

Also worth mentioning is that the DS.FixtureAdapter does not replace nor have the same feature set as the DS.RESTAdapter , it's only meant to provide simple mockup data for your app, so switching between adapter's would not work in case of the DS.FixtureAdapter & DS.RESTAdapter . Use DS.FixtureAdapter when you don't yet care to communicate with a backend, but will store your data as "fixtures" in the client.

Hope it helps.

@intuitivepixel's answer no longer works in current day Ember (v1.0+); as registerAdapter no longer exists in the API.

Nowadays you can just declare an Adapter for each model where you want to override the adapter choice:

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

App.LegacyAdapter = DS.FixtureAdapter;

@gonvaled it might be because you used

DS.Store.registerAdapter('App.LegacyModel', App.MyLegacyAdapter.extend({});

and not

App.Store.registerAdapter('App.LegacyModel', App.MyLegacyAdapter.extend({});

I add the same problem and by using App.Sotre it was all fixed.

For my use case i needed 2 different adapters for the same model. To save all my products viewed from the rest adapter to be visible offline on an history page using the fixture adapter and sql lite.

App.Store = DS.Store.extend({
    revision: 13,
    adapter: DS.RESTAdapter.create({
        url: app.apiUrl,
        namespace: app.apiNamespace
    })
});


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

App.Producthistory = App.Product;

App.Store.registerAdapter('App.Producthistory', DS.FixtureAdapter);

App.HistoriqueRoute = Ember.Route.extend({
    setupController: function(controller, model) {
    controller.set('model', App.Producthistory.find());
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM