Engine#initializer()

initializer (initializer) public

Inherited from Ember.Engine but overwritten in packages/ember-application/lib/system/engine.js:185

The goal of initializers should be to register dependencies and injections. This phase runs once. Because these initializers may load code, they are allowed to defer application readiness and advance it. If you need to access the container or store you should use an InstanceInitializer that will be run after all initializers and therefore after all code is loaded and the app is ready.

Initializer receives an object which has the following attributes: name, before, after, initialize. The only required attribute is initialize, all others are optional.

  • name allows you to specify under which name the initializer is registered. This must be a unique name, as trying to register two initializers with the same name will result in an error.
Ember.Application.initializer({
  name: 'namedInitializer',

  initialize: function(application) {
    Ember.debug('Running namedInitializer!');
  }
});
  • before and after are used to ensure that this initializer is ran prior or after the one identified by the value. This value can be a single string or an array of strings, referencing the name of other initializers.

An example of ordering initializers, we create an initializer named first:

Ember.Application.initializer({
  name: 'first',

  initialize: function(application) {
    Ember.debug('First initializer!');
  }
});

// DEBUG: First initializer!

We add another initializer named second, specifying that it should run after the initializer named first:

Ember.Application.initializer({
  name: 'second',
  after: 'first',

  initialize: function(application) {
    Ember.debug('Second initializer!');
  }
});

// DEBUG: First initializer!
// DEBUG: Second initializer!

Afterwards we add a further initializer named pre, this time specifying that it should run before the initializer named first:

Ember.Application.initializer({
  name: 'pre',
  before: 'first',

  initialize: function(application) {
    Ember.debug('Pre initializer!');
  }
});

// DEBUG: Pre initializer!
// DEBUG: First initializer!
// DEBUG: Second initializer!

Finally we add an initializer named post, specifying it should run after both the first and the second initializers:

Ember.Application.initializer({
  name: 'post',
  after: ['first', 'second'],

  initialize: function(application) {
    Ember.debug('Post initializer!');
  }
});

// DEBUG: Pre initializer!
// DEBUG: First initializer!
// DEBUG: Second initializer!
// DEBUG: Post initializer!
  • initialize is a callback function that receives one argument, application, on which you can operate.

Example of using application to register an adapter:

Ember.Application.initializer({
  name: 'api-adapter',

  initialize: function(application) {
    application.register('api-adapter:main', ApiAdapter);
  }
});

Parameters:

initializer Object
doc_EmberJs
2016-11-30 16:51:51
Comments
Leave a Comment

Please login to continue.