同时使用DS.FixtureAdapter和DS.RESTAdapter [英] Using DS.FixtureAdapter and DS.RESTAdapter at the same time

查看:89
本文介绍了同时使用DS.FixtureAdapter和DS.RESTAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用两个适配器,具体取决于路由。目前我有以下商店:

  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



但是我想要有两个商店。是否可以这样做?有人可以提供一个如何配置路由器的例子?

解决方案


但是我会喜欢是有两家商店。是否可以这样做,在路线上?


根据设计,您不应该想要有多个商店在你的应用程序但是,存在



这样可以使用

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

如果您有多个型号可以使用相同的适配器,请执行以下操作:

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

当提出请求时,商店将使用型号对应的适配器。



您仍然可以指定一个

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

如果您尝试加载没有为其类型指定的适配器的记录,则此默认适配器将用作回退。



按照最佳做法,如果你想使用除了常规查找的模型以外的其他模型(例如,使用你的路线的名字)你可以这样做:

  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 ,但会将您的数据作为夹具存储在客户端。



希望有帮助。


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

这篇关于同时使用DS.FixtureAdapter和DS.RESTAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆