Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ENV values in ember-cli app during build

I would like to set my RESTAdapter host based on the build environment.

I assume the value can be stored in config/environment.js like this:

if (environment === 'development') {
  ENV.API_ENDPOINT = 'http://localhost:8080';
}

if (environment === 'production') {
  ENV.API_ENDPOINT = 'http://api.myserver.com';
}

But I am unsure how to insert the information into adapter/application.js during the build process.

like image 390
Weston Avatar asked Aug 07 '14 17:08

Weston


1 Answers

You define the setting like this in your config/environment.js:

  // snip
  APP: {
    // Here you can pass flags/options to your application instance
    // when it is created
    API_HOST: 'http://192.168.1.37:3000' // default setting
  }
};

if (environment === 'development') {
  ENV.APP.LOG_TRANSITIONS = true;
  ENV.APP.API_HOST = 'http://192.168.1.37:3000'; // override
}

You can then use the setting in other files like this:

// app/adapters/application.js:
import DS from "ember-data";

export default DS.RESTAdapter.extend({
   host: window.MyAppENV.APP.API_HOST
});

Replace MyApp with your application.

You switch to a build environment with the ember --environment option:

ember serve --environment production

or

ember build --environment development

I've not seen yet whether there is a way to provide the value dynamically, but you can provide as many environments as you want of course.

Update: Adding for completeness, and as per Weston's comment, Environments documents this functionality.

like image 90
Leeft Avatar answered Sep 27 '22 21:09

Leeft