Reputation: 208
As I am new to ember, I got confused between the application initializer and a service. To my knowledge, both of them was doing the same job and both have the lifetime of the application. Could anyone please explain the difference between them with a clear example?
Upvotes: 5
Views: 910
Reputation: 519
Application Initializer is a place where we initialize or Register or inject our new factory classes or any Services. And Both services and Application Initializer initialized class(factory) can share their states like a singleton to every routes they were used . Using applicationInstance.lookup() method we can even use any Application Initializer initialized class in which they were'nt injected initially when they were created and can share their states as well
Upvotes: 2
Reputation: 65173
Services can be injected into other areas of your app, where as application initializers cannot.
Services are useful for when you want to track app-level state, such as whether or not a sidebar is open/closed -- or if you want to manage the open/closed state of a modal -- or if you want to manage a websocket connection. Because Services are injectable, they allow the other areas of your application to interact with the services' functions, properties, etc.
Application Initializers are actually only run during boot, during the initialization of the _application. A common pattern here is if you want to inject a service into all routes or something.
Docs on Initializers: https://guides.emberjs.com/release/applications/initializers/
Docs on Services: https://guides.emberjs.com/release/applications/services/
Upvotes: 4