Search code examples
ember.jsember-cli

EmberJs; conditionally start instance initializer


Is it possible to conditionally run an instance initializer based on an environment variable?

The use case: I have an Ember app running on my local machine. If a certain environment variable is present when the app starts, I want to start a service called "lights". If it's not present I do not want the service to run.

//instance-intializers/lights.js
 export default {
  name: 'lights',
  initialize(application) {
  application.container.lookup('service:lights');
 }
};

Solution

  • A nice way is to remove it from build. (As Lux mentioned in his answer.)

    A quick way is to get environment variable and do a check such as :

    //instance-intializers/lights.js
    export default {
      name: 'lights',
      initialize(application) {
        let env = application.resolveRegistration('config:environment');
        if(env === ''){
          application.lookup('service:lights');
          //... do whatever you want...
        }
      }
    };