Engine#instanceInitializer()

instanceInitializer (instanceInitializer) public

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

Instance initializers run after all initializers have run. Because instance initializers run after the app is fully set up. We have access to the store, container, and other items. However, these initializers run after code has loaded and are not allowed to defer readiness.

Instance 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 instanceInitializer is registered. This must be a unique name, as trying to register two instanceInitializer with the same name will result in an error.
Ember.Application.instanceInitializer({
  name: 'namedinstanceInitializer',

  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.

  • See Ember.Application.initializer for discussion on the usage of before and after.

Example instanceInitializer to preload data into the store.

Ember.Application.initializer({
  name: 'preload-data',

  initialize: function(application) {
    var userConfig, userConfigEncoded, store;
    // We have a HTML escaped JSON representation of the user's basic
    // configuration generated server side and stored in the DOM of the main
    // index.html file. This allows the app to have access to a set of data
    // without making any additional remote calls. Good for basic data that is
    // needed for immediate rendering of the page. Keep in mind, this data,
    // like all local models and data can be manipulated by the user, so it
    // should not be relied upon for security or authorization.
    //
    // Grab the encoded data from the meta tag
    userConfigEncoded = Ember.$('head meta[name=app-user-config]').attr('content');
    // Unescape the text, then parse the resulting JSON into a real object
    userConfig = JSON.parse(unescape(userConfigEncoded));
    // Lookup the store
    store = application.lookup('service:store');
    // Push the encoded JSON into the store
    store.pushPayload(userConfig);
  }
});

Parameters:

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

Please login to continue.