From 13eb46a4e0c9804b050d30368b6f934fcc2638c7 Mon Sep 17 00:00:00 2001 From: opsb Date: Tue, 4 Nov 2014 13:45:49 -0600 Subject: [PATCH] Add initializers guide --- data/guides.yml | 4 ++ source/guides/initializers/index.md | 98 +++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 source/guides/initializers/index.md diff --git a/data/guides.yml b/data/guides.yml index 83bfe94148..6fba2d3ed1 100644 --- a/data/guides.yml +++ b/data/guides.yml @@ -233,6 +233,10 @@ Enumerables: - title: "Introduction" url: "enumerables" +Initializers: + - title: "Introduction" + url: "initializers" + Testing: - title: "Introduction" url: "testing" diff --git a/source/guides/initializers/index.md b/source/guides/initializers/index.md new file mode 100644 index 0000000000..f77a6ce867 --- /dev/null +++ b/source/guides/initializers/index.md @@ -0,0 +1,98 @@ +## Using initializers + +When an Ember.js application starts up you often want to configure things like 3rd party libraries. Ember.js allows you to register initializers that will be called when the app starts up. + + +```javascript +Ember.Application.initializer({ + name: "initializerName", + + initialize: function(_, application) { + // your initialization code + } +}); +``` + +A basic initializer requires two things, a name (which allows initializer ordering, see below) and an initialize function containing the code you want to run at startup time. The initialize function makes the application available for dependency injection (_the first argument is the container but it's use is considered harmful and should not be used_). + + +### Initializer ordering +It's often the case that your initializer must be run after another initializer e.g. your initializer needs access to the ember-auth to configure it. Ember allows you to specify 'before' and 'after' so that you may dictate the order in which the initializers are run. + +```javascript +App.initializer({ + name: "first", + initialize: function(_, application) { + // called first + } +}); + +App.initializer({ + after: "first", + name: "second", + before: "last", + + initialize: function(_, application) { + // called after first but before last + } +}); + +App.initializer({ + name: "last", + + initialize: function(_, application) { + // called after second + } +}); +``` + + +### Initializers for multiple ember apps + +If you're loading multiple ember apps on the same page you may specify different initializers for each app. Note that for this to work you need to create a subclass of Ember.Application for each of your apps. + +```javascript +AppABase = Ember.Application.extend(); +AppBBase = Ember.Application.extend(); + +AppABase.initializer({ + name: "appAInitializer", + initialize: function(_, application){ + + } +}); + +AppBBase.initializer({ + name: "appBInitializer", + initialize: function(_, application){ + + } +}); + +AppA = AppABase.create({...}); +AppB = AppBBase.create({...}); +``` + + +### Initializing services + +If you have any additional services that you want to inject into controllers or routes then an initializer is the place to do it. + +```javascript +Ember.Application.initializer({ + name: 'logger', + + initialize: function(_, application) { + var logger = { + log: function(m) { + console.log(m); + } + }; + + application.register('logger:main', logger, { instantiate: false }); + application.inject('route', 'logger', 'logger:main'); + } +}); +``` + +For more details see [Dependency injection & service lookup](/guides/understanding-ember/dependency-injection-and-service-lookup). \ No newline at end of file