Lopo
Lopo

Reputation: 966

ember cli environment configuration

Could someone please provide an example for this. I am not getting this to work.

http://www.ember-cli.com/user-guide/#Environments

I have set an environment variable in my config/environment.js file. In a route action I want to access this variable. But ENV is undefined.

environment.js

if (environment === 'test') {
   ENV.APP.REPORTING_SERVICE_URL = 'http://www.purplerow.com';
}
if (environment === 'production') {
   ENV.APP.REPORTING_SERVICE_URL = 'http://www.stackoverflow.com';
}

Route

import ENV from 'my-demo-app/config/environment';

export default Ember.Route.extend({
   actions: {
     doSomething: function() {
       console.log(ENV.REPORTING_SERVICE_URL); // ENV is undefined
     }
   }
});

Upvotes: 0

Views: 459

Answers (2)

Kuba Niechciał
Kuba Niechciał

Reputation: 974

You are setting your variable this way:

ENV.APP.REPORTING_SERVICE_URL = 'http://www.purplerow.com';

But accessing it this way:

ENV.REPORTING_SERVICE_URL

You should either remove APP when setting or add it while getting, e.g.:

ENV.APP.REPORTING_SERVICE_URL

If it is undefined, make sure that you did not introduced any syntax that may break the environment. Basically, config/environment.js should look something like this:

module.exports = function(environment) {
  var ENV = {
    modulePrefix: "my-demo-app",
    environment: environment,
    baseURL: "/",
    locationType: "auto"
  };
  return ENV;
};

Take notice that the name of the variable that you export (in this case it's ENV) does not have to match the variable name that you import. It should just import everthing that is exported in the file and put it into the variable you named in import. If it is undefined, you basically export undefined.

Upvotes: 1

Artur Smirnov
Artur Smirnov

Reputation: 804

Please make sure that ENV is returned in your ./config/environment.js.

In general it should look like follows:

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'app',
    environment: environment,
    baseURL: '/',
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. 'with-controller': true
      }
    },

    APP: {
      // Here you can pass flags/options to your application instance when it is created
      // Also put default values here

      REPORTING_SERVICE_URL = 'http://www.stackoverflow.com'
    }
  };

  // overwrite default values for different environments as needed

  if (environment === 'development') {

  }

  if (environment === 'test') {
    ENV.APP.REPORTING_SERVICE_URL = 'http://www.purplerow.com';
  }

  if (environment === 'production') {

  }

  return ENV;
};

Then you can use it in your application code:

import ENV from './config/environment';

export default Ember.Route.extend({
   actions: {
     doSomething: function() {
       console.log(ENV.APP.REPORTING_SERVICE_URL);
     }
   }
});

Upvotes: 1

Related Questions