Open In App

Ember.js Application instanceInitializer() Method

Improve
Improve
Like Article
Like
Save
Share
Report

Ember.js is a JavaScript web framework that utilizes a component-service pattern. It is Open-Sourced. The Ember Js Applications when booted up can use initializers to set up the environment and its functionality by the user. The InstanceInitializers are used to initialize different instances of the application. They are executed after the initializers of the application have been executed.

Syntax:

ember generate instance-initializer <initializer-name>

Attributes: The InstanceInitializer object has four attributes that are used to define its execution process:

  1. name: this is used to define the name of the instance-initializer. This must be unique.
  2. before: this is used to ensure that the current instance-initializer is run before the given instance-initializer
  3. after: this is used to ensure that the current instance-initializer is run after the given instance-initializer
  4. initialize: this is used to call the initialize() function of the instance-initializer object.

Method:

  • initialize( ): It is the function where the code which is to run during this instance-initializer process is specified.

Create An Ember App: Now we need to create an Ember app. Go to the directory where you want to save the application and run the command:

ember create my-app

Create Ember Application

Example 1: Now we will create an instance-initializer, so run the command:

ember generate instance-initializer instance1

Now copy the following code into the instance1.js file created in the instance-initializers folder.

instance1.js: Here We are first importing the ‘debug’ function which we will use to output our progress. Next is the initialize function which describes what steps to perform during the initialization of this object. Third and final part is setting default properties like name and the initialize function.

Javascript




import { debug } from '@ember/debug';
 
export function initialize() {
  debug('Now Running instance-initializer instance1!');
}
 
export default {
  name: 'instance1',
  initialize,
};


Step to run the application: Run the command:

 ember server

Output: Go to localhost:4200, open the console, and check the debug log

Instance-Initializer Output 1

Example 2: Now we will use ‘before’ and ‘after’ attributes to define the order in which the instance initializers are executed. So Run the following commands:

 ember generate instance-initializers instance2
 ember generate instance-initializers instance3

Now copy the following codes in instance2.js and instance3.js files respectively.

intance2.js: Here We are first importing the ‘debug’ function which we will use to output our progress. Next is the initialize function which describes what steps to perform during the initialization of this object. The third and final part is setting default properties like name and after and the initialize function. The ‘after’ attribute contains the name of the initializer after which this object is to be initialized.

Javascript




import { debug } from '@ember/debug';
 
export function initialize() {
  debug('Now Running instance-initializer instance2!');
}
 
export default {
  name: 'instance2',
  after: 'instance1',
  initialize,
};


instance3.js: Here We are first importing the ‘debug’ function which we will use to output our progress. Next is the initialize function which describes what steps to perform during the initialization of this object. Third and final part is setting default properties like name and before and the initialize function. The ‘before’ function is used to refer to the initializer before which this object must be initialized.

Javascript




import { debug } from '@ember/debug';
 
export function initialize() {
  debug('Now Running instance-initializer instance3!');
}
 
export default {
  name: 'instance3',
  before: 'instance1',
  initialize,
};


Step to run the application: Run the command 

ember server

Output: Now go to localhost:4200 and check the console log.

Instance Initializer Output 2



Last Updated : 21 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads