Perhaps this is a little esoteric but I need some help.
My use case is a clock ticker. I want a service that upon init()
will start a timer. (for example an AJAX polling service). I want this to start at app boot but I don't wish to inject it into every object type.
What I've tried:
Ember.inject.service()
in the application controller.app.register(…, MyService.create(), {instantiate: false})
without calling app.inject(…)
.init()
instead Ember.inject.service()
it into the application route/controller and in its init()
call this.get('myService').startTimer()
.Here are some of the stumbling blocks I've run into:
this.get('myService')
. I could do that in the controller's init()
but that felt like a code smell.services/my-service.js
file and auto register it. Performing an app.register()
seems to register two instances and the two get confused.init()
this solution also felt like a code smell. But of all the solutions I've tried this works and is the least smelly of the three.Is there any other alternatives?
TL;DR Use an instance initializer
An instance initializer will have the needed lookup functions to fetch a service that the system auto registered and perform an action on it.
However, it may be more appropriate to save this initialization for a route or controller's init()
because any ajax like thing here will still fall under Embers Loading state and run loop. While being performed in an instance initializer will degrade boot performance with no actual benefits.
If you still feel the initializer is the way to go here is a contrived example that is both Ember 1.13 and 2.0 compatible:
// app/instance-initializers/start-my-service.js
export function initialize(app) {
const { container = app } = app;
const myService = container.lookup('service:my-service');
myService.startPolling();
}
export default {
initialize
};
An example ember-twiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With