Create an initialization script in EmberJS

advertisements

Emberjs beginner here.

I have an Emberjs app where I need to execute an initialize function after jquery has been loaded.

Where would I put the said initialize function and how would I call it?


You are looking for Ember initializers.

Ember.Application.initializer({

    name: 'my-initializer',
    after: 'some-other-initializer',

    initialize: function(container, application) {
        application.deferApplicationReadiness();
        application.register(...);

        very_long_initialization_task_returning_promise().then(function() {
            application.advanceApplicationReadiness();
        });
    }
});

You can use initializers to preload something in the store, organize dependency injections, etc. The after property provides the ability to set the order for the initializers. deferApplicationReadiness and advanceApplicationReadiness allow you to tell Ember when the application is ready to go.

For more details see http://emberjs.com/api/classes/Ember.Application.html#method_initializer.

If you are using ember-cli, make the hash passed to Ember.Application.initializer above the default export from a file named initializers/my-initializer.js:

export default {
    name: 'my-initializer',
    ...
}