This feature is only available from Ember 1.10.0+.
Some examples when services could be used are:
- Interfacing with a geolocation API
- Coordinating drag-and-drop events (between components for example)
- Consuming push events from a server
- Using a non-CRUD server API
To create a player service type ember generate service player
or create a file manually:
|
|
Services are automatically detected and registered by placing them in the services directory.
To implement a service, just export a subclass of Ember.Service
|
|
To use it in components
(or routes
, controllers
etc.) or any other Ember Object
:
|
|
The name passed into the service function is not required if property name matches the service name, so it is often omitted and not written like this:
|
|
Services can then be used in the templates like so (replace < >
with double curly braces (handlebars syntax):
|
|
or with helpers:
|
|
Services are only instantiated once. This means that there is instance of the service that is never destroyed as long as your application is running.