Simple template helper to inject services into templates.
ember install ember-service-helper
There are two ways to invoke the {{service}}
helper.
{{service serviceName}}
— Returns the service itself.
Like callingowner.lookup(`service:${serviceName}`)
{{service serviceName methodName}}
— Returns the method, bound to the instance.
Example using the built-in {{get}}
helper and
ember-responsive
. Note that
{{get}}
returns a bound reference.
Example using ember-set-helper
.
Example using the
{{pick}}
helper from ember-composable-helpers
to get the event.target.checked
property.
export default class ThemeService extends Service {
@tracked isDark = false;
toggleDarkMode(newValue = !this.isDark) {
// Even though this method isn't using `@action`, the `{{service}}` helper
// binds it to the service instance.
this.isDark = newValue;
}
}