项目作者: buschtoens

项目描述 :
Simple template helper to inject services into templates
高级语言: JavaScript
项目地址: git://github.com/buschtoens/ember-service-helper.git
创建时间: 2019-09-13T21:25:41Z
项目社区:https://github.com/buschtoens/ember-service-helper

开源协议:MIT License

下载


ember-service-helper

CI
npm version
Download Total
Ember Observer Score
code style: prettier
dependencies
devDependencies

Simple template helper to inject services into templates.

Installation

  1. ember install ember-service-helper

Usage

There are two ways to invoke the {{service}} helper.

  • {{service serviceName}} — Returns the service itself.

    Like calling owner.lookup(`service:${serviceName}`)
  • {{service serviceName methodName}} — Returns the method, bound to the instance.

Properties

Getting Properties

Example using the built-in {{get}} helper and
ember-responsive. Note that
{{get}} returns a bound reference.

  1. {{#if (get (service "breakpoints") "isDesktop")}}
  2. Desktop breakpoint
  3. {{else}}
  4. Mobile breakpoint
  5. {{/if}}

Setting Properties

Example using ember-set-helper.

  1. <ColorPicker @update={{set (service "preferences") "favoriteColor"}}>

Methods

Example using the
{{pick}} helper from ember-composable-helpers
to get the event.target.checked property.

  1. <label>
  2. Enable dark mode
  3. <input
  4. type="checkbox"
  5. checked={{get (service "theme") "isDark"}}
  6. {{on "input" (pick "target.checked" (service "theme" "toggleDarkMode"))}}
  7. >
  8. </label>
  1. export default class ThemeService extends Service {
  2. @tracked isDark = false;
  3. toggleDarkMode(newValue = !this.isDark) {
  4. // Even though this method isn't using `@action`, the `{{service}}` helper
  5. // binds it to the service instance.
  6. this.isDark = newValue;
  7. }
  8. }