每个Ember环境保留多个适配器的灯具

时间:2014-07-23 05:58:24

标签: javascript ember.js ember-data ember-cli

虽然自然进展replace the fixture adapter with another adapter,但我希望保留用于开发环境的灯具,同时利用不同的Ember数据适配器进行制作。

这是由于:

  • 渐进式增强的重复迭代
  • 打算从iOS应用程序中嵌入UIWebView,生产配置与本地程序集的桥接调用紧密结合。

Ember CLI资产编译基于broccoli加载Web或Native API:

app.import({
    development: 'vendor/company/WebAPI.js',
    production: 'vendor/company/NativeAPI.js'
});

但是,我不确定如何利用此模式更改适配器。

开发环境

对于开发,我想使用模拟数据或http服务来启用浏览器测试。

因此,在开发环境中启动Ember服务器可以利用灯具。

ember server --environment=development

此配置将扩展FixtureAdapter以进行开发:

var ApplicationAdapter = DS.FixtureAdapter.extend({
    /* ... */
});
export default ApplicationAdapter;

生产环境

然而,复杂性是需要不同适配器的生产环境。

在生产中启动Ember服务器时,服务通过bridge://方案提供,其中本机iOS应用程序管理传输层和数据模型。

ember server --environment=production

此配置会扩展生产的基础Adapter

var ApplicationAdapter = DS.Adapter.extend({
    /* ... */
});
export default ApplicationAdapter;

如何在Ember应用程序中使用多个适配器?如何在App中交换适配器,或者路由中的​​商店是否会定义不同的适配器?

1 个答案:

答案 0 :(得分:6)

您可以提供一个全局应用程序适配器,它将了解您的环境并根据当前环境导出最适合的适配器(如果需要,可以与序列化程序相同):

+-- app
|   |-- adapters
|   |   `-- application.js
|   |   `-- custom-adapter.js
|   |-- ...
|   | 
|   |-- serializers
|       `-- application.js
|       `-- custom-serializer.js

的application.js:

import DS from 'ember-data';
import config from '../config/environment';
import CustomAdapter from './custom-adapter';

var adapter = DS.FixtureAdapter.extend({});

if (config.environment === 'production') {
    adapter = CustomAdapter;
}

export default adapter;

custom-adapter.js

import Ember from 'ember';
import DS from 'ember-data';

var adapter = DS.RESTAdapter.extend({
    // your custom RESTAdapter
});

export default adapter;

希望它会有所帮助

相关问题