Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retain fixtures with multiple adapters per Ember environment

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?

like image 856
Jason Sturges Avatar asked Jul 23 '14 05:07

Jason Sturges


1 Answers

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

like image 154
bmeurant Avatar answered Nov 04 '22 05:11

bmeurant