makz
makz

Reputation: 51

Ember, inject a service application wide

I know this question is already posted but i don't find any updated or well explained response.

So i have a service "session" & "current-user" who have my session & current user (obvious) in memory and i need to have this service in every controllers, actually, in every controller i do

session: service(),
currentUser: service('current-user'),

My question is, how to make those services available in all controller without redeclare them in every controllers ?

Thank you.

Upvotes: 1

Views: 416

Answers (1)

makz
makz

Reputation: 51

Okay i had to post a question to finally find a response ...

So i used initializers, let's do it for my current-user service.

ember g initializer current-user

It create a new file in app/initializers/current-user.js and i put this into it

export function initialize(application) {
  application.inject('route', 'currentUser', 'service:current-user');
  application.inject('controller', 'currentUser', 'service:current-user');
  application.inject('component', 'currentUser', 'service:current-user');
}

export default {
  name: 'current-user',
  initialize
};

In this code exemple, i inject the current-user service (thrid argument) inside routes, controllers & components (first argument) and it'll be named currentUser (second argument).

I hope it will help other ppl :)

Upvotes: 1

Related Questions