简体   繁体   中英

Inject service into a utility Ember-cli

Is it possible to access an Ember service from an Ember utility? I can't find much of anything about utilities in the docs. Or about services for that matter.

I'm using Ember-cli 0.2.7 and Ember 1.12.0

Well, Ember utility is just a exported JavaScript method, while Ember.Service is part of Ember ecosystem. To access Ember.Service from utility you would have to pass reference to concrete Ember.Service to utility which is probably bad design decision.

If you could explain what do you need utility and service for it would probably be easier to suggest correct way to go, without passing directly reference to Ember.Service to JavaScript method.

You can use Ember.Service when you have some logic / states that need to be shared across many controllers / routes. For example, you can define sound service, and then from many places use public methods you will write/expose. You can make sure that only 1 song plays at the moment. That's service for. Utility can be helpful for example to create some async pattern, like specific way of iterating over promises, like mapSeries - it is universal and can be used in many places. Thanks to requiring it as utility your code is DRY and you import it where you need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM