Allow to disable a initializer and its dependents

Hey,

would it be desirable to have an option to disable initializers?

I have a bunch of initializers of which some are disabled in certain environments (for example Piwik stats in development mode). So I see myself writing code like this over and over again:

var piwikEnabled = MyProjectENV.ENABLE_ANALYTICS;
if (piwikEnabled) {
    Ember.debug('Init Piwik');
} else {
    Ember.debug('Skip Piwik');
    return;
}
[...]

This gets even worse if you have dependent initializers and need to check for “basis” features to be enabled. So what I am suggesting here, is to add a enabled boolean flag to the initializer object:

export default {
    name: 'pipeline',
    enabled: MyProjectEnv.ENABLE_PIPELINE,
    initialize: function (container, application) {
        [...]
    }
}

export default {
    name: 'task-xyz',
    after: 'pipeline'
    initialize: function (container, application) {
        [...]
    }

Then if you set MyProjectEnv.ENABLE_PIPELINE to false it would disable the pipeline initializer and automatically the task-xyz as we already know it depends on the pipeline because of the after specification. As Ember already builds up a dependency graph it should be possible to cut of the whole branch of a disabled node.

What do you think? Is there a chance to see this go upstream if I worked on it?

Best regards, Dominik

I’m guessing you’re using ember-cli and so this all passes through the ember-load-initializers module.

So you can easily fork this load-initializer and replace it with:

(function() {
define("ember/load-initializers",
  [],
  function() {
    "use strict";

    return {
      'default': function(app, prefix) {
        var initializersRegExp = new RegExp('^' + prefix + '/initializers');

        Ember.keys(requirejs._eak_seen).filter(function(key) {
          return initializersRegExp.test(key);
        }).forEach(function(moduleName) {
          var module = require(moduleName, null, null, true);
          if (!module) { throw new Error(moduleName + ' must export an initializer.'); }
          if(module['default'].enabled) { app.initializer(module['default']); }
        });
      }
    }
  }
);
})();

As Ember already builds up a dependency graph it should be possible to cut of the whole branch of a disabled node.

You’ll need to get crafty there. But that shouldn’t be difficult either. But I’m suspecting that if you don’t register an initializer and you have initializers dependent to trigger after it… then those initializers shouldn’t be triggered. I can’t really validate that right now, but maybe tonight.