Retain fixtures with multiple adapters per Ember environment

回眸只為那壹抹淺笑 提交于 2019-12-08 19:21:33

问题


Although the natural progression would replace the fixture adapter with another adapter, I'd like to retain fixtures for development environment while leveraging a different Ember data adapter for production.

This is due to:

  • Heavy iterations of progressive enhancement
  • Intended to run embedded in a UIWebView from an iOS app, production configuration is tightly coupled with bridged calls to native assembly for data.

Ember CLI asset compilation is based on broccoli to load either a Web or Native API:

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

However, I'm unsure how to leverage this pattern to change adapters.

Development Environment

For development, I want to use mock data or http services to enable testing in browser.

Therefore, launching Ember server in development environment leverages fixtures.

ember server --environment=development

This configuration would extend FixtureAdapter for development:

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

Production Environment

However, the complexity is production environment where different adapters are needed.

When launching Ember server in production, services are provided via a bridge:// scheme where the native iOS app is managing transport layer and data models.

ember server --environment=production

This configuration would extend the base Adapter for production:

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

How can multiple adapters be used in an Ember app? How is the adapter swapped in the App, or perhaps the store from the route would define a different adapter?


回答1:


You could provide a global application adapter that will be aware of your environment and export the best suited adapter depending on the current environment (same thing for your serializer if needed):

+-- 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;

Hope it will help



来源:https://stackoverflow.com/questions/24902918/retain-fixtures-with-multiple-adapters-per-ember-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!