Using dependency injection

The dependency injection pattern is used to declare and instantiate classes of objects and handle dependencies between them. In Ember, we can take objects or services and inject them into routes, controllers, or components.

In this recipe, we'll take a logger object and inject it into our controllers using dependency injection.

How to do it...

  1. In a new application, install the moment library and create a new initializer:
    $ bower install moment –save
    $ ember g initializer application
    

    This will install the Bower Moment.js library. We'll use this for our custom logger.

  2. Import the Moment library to the Ember project:
    // ember-cli-build.js /*jshint node:true*/ /* global require, module */ var EmberApp = require('ember-cli/lib/broccoli/ember-app'); ...

Get Ember.js Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.