同时使用 DS.FixtureAdapter 和 DS.RESTAdapter [英] 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
是一个 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.
这看起来像这样:
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());
}
});
另外值得一提的是,DS.FixtureAdapter
并没有取代 DS.RESTAdapter
也没有相同的功能集,它只是为了提供简单的模型数据您的应用程序,因此在 DS.FixtureAdapter
的情况下无法在适配器之间切换 &DS.RESTAdapter
.当您不关心与后端通信但会将您的数据作为夹具"存储在客户端时,请使用 DS.FixtureAdapter
.
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.
希望有帮助.
这篇关于同时使用 DS.FixtureAdapter 和 DS.RESTAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!