Injection function with initializer in ember-cli

I have an application based on Ember-cli. I am trying to inject a method into all routes, controllers and views. I know that I could use the directory app/utilsand import the method module into all the files that call it, but I would like this method to be automatically available. Therefore, I decided to introduce a method using an initializer.

The initializer looks like this:

export default {
  name: 'injectMethod',

  initialize: function(container, app) {
    var someFunction = function(message) {

    };

    app.register('function:main', someFunction);

    Em.A(['route', 'controller', 'view']).forEach(function(place) {
      app.inject(place, 'someFunction', 'function:main');
    });
  }
};

As a result, you receive the following error message: Uncaught TypeError: undefined is not a function. The error will disappear when I delete the line app.inject().

Are initializers handled differently in ember-cli and / or is something wrong in the above code? Or is this a better way to achieve my goal than using an initializer?

+4
1

Ember , factory, create. ( ), Ember, , .

export default {
  name: 'injectMethod',

  initialize: function(container, app) {
    var someFunction = function(message) {

    };

    app.register('function:main', someFunction, {instantiate: false});

    Em.A(['route', 'controller', 'view']).forEach(function(place) {
      app.inject(place, 'someFunction', 'function:main');
    });
  }
};

: http://emberjs.jsbin.com/xaboliwu/1/edit

+11

All Articles