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:
- Writing a service and using
Ember.inject.service()in the application controller. - Use an initializer with
app.register(…, MyService.create(), {instantiate: false})without callingapp.inject(…). - Don't start the timer in the
init()insteadEmber.inject.service()it into the application route/controller and in itsinit()callthis.get('myService').startTimer().
Here are some of the stumbling blocks I've run into:
- Services are lazy loaded and so the timer never starts because the application controller never performed a
this.get('myService'). I could do that in the controller'sinit()but that felt like a code smell. - Seems that the ember resolver will see the
services/my-service.jsfile and auto register it. Performing anapp.register()seems to register two instances and the two get confused. - Like getting the lazy service in the application controller's
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?
Aucun commentaire:
Enregistrer un commentaire