mardi 12 juillet 2016

How do I initialize an Ember service at app boot without type injection?

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:

  1. Writing a service and using Ember.inject.service() in the application controller.
  2. Use an initializer with app.register(…, MyService.create(), {instantiate: false}) without calling app.inject(…).
  3. Don't start the timer in the 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:

  1. 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's init() but that felt like a code smell.
  2. Seems that the ember resolver will see the services/my-service.js file and auto register it. Performing an app.register() seems to register two instances and the two get confused.
  3. 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