Skip to content

Inject anything from your Ember container into any class as a property. (i.e. sort of like `needs` for everything else)

License

Notifications You must be signed in to change notification settings

pivotshare/ember-computed-injection

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ember-computed-injection

A computed property for injecting any dependency into an Ember class definition. Sort of like needs for everything else. Based off on-going discussion about a possible Service convention, but not dependent on it what-so-ever. You can inject any object type you want! In fact, you can even use Ember.computed.injection as a replacement for needs all together.

Usage

ES5 (traditional)

App.ExampleController = Ember.Controller.extend({
  geolocation: Ember.computed.injection('service:geolocation')
});

This assumes you have some existing class defined like this:

App.GeolocationService = Ember.Object.extend();

Notice that Ember will resolve it service:geolocation => App.GeolocationService just like it would controller:example => App.ExampleController.

By default, injections will return a singleton. You can provide an options hash to change this behavior:

App.ExampleController = Ember.Controller.extend({
  geolocation: Ember.computed.injection('service:geolocation', { singleton: false })
});

Alternatively, you can globally change this behavior using an initializer:

Ember.onLoad('Ember.Application', function (Application) {
  Application.initializer({
    name: 'services',

    initialize: function (container, application) {
      // App.GeolocationService is some hypothetical class you defined prior 
      application.register('service:geolocation', App.GeolocationService, { singleton: false });
    }
  });
});

ES6 Modules (ember-cli/Ember App Kit)

var injection = Ember.computed.injection;

var ExampleController = Ember.Controller.extend({
  geolocation: injection('service:geolocation')
});

export default ExampleController;

Since both ember-cli and Ember App Kit use a customer Resolver, all you need to do is place the item you are injecting inside a pluralized directory name of the same type. e.g. app/services/geolocation.js

See the ES5 usage for more info

Any gotchas?

Only for users who stray outside the Ember-idiomatic beaten path and manually create class instances.

Instances must have an Ember.Container assigned at this.container. Ember will handle this for you when resolving routes, controllers, views, etc but if you manually .create() a class, it does not come with a container, so it won't be able to resolve your dependency. You can either pass an existing container along or use the container itself to create your class instances.Learn more

License

MIT Licensed

About

Inject anything from your Ember container into any class as a property. (i.e. sort of like `needs` for everything else)

Resources

License

Stars

Watchers

Forks

Packages

No packages published