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

后端 未结 3 1424
星月不相逢
星月不相逢 2021-01-14 11:08

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.FixtureAdap         


        
3条回答
  •  一生所求
    2021-01-14 11:13

    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 (e.g. 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.

提交回复
热议问题