Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/gossi/ember-command. Integrate your Business Logic with Ember gossi.github.io/ember-command
Something went wrong. Try again.
# Actions
Actions are perfect to connect existing functions with the Ember's DI container
or to wrap your service calls in functions.
## Wrapping Service Calls
Here is a `Counter` service, which can increment and decrement. Let's connect it
with a single-file-component.
::: info
The best part: `action()` and
[`ability()`](https://github.com/gossi/ember-ability) share the same API, so you
only need to learn one.
:::
::: code-group
```gts [components/counter.gts]
import { action } from 'ember-command';
import { ability } from 'ember-ability';
import { on } from '@ember/modifier';
const inc = action(({ services }) => () => {
services.counter.inc();
});
const count = ability(({ services }) => () => {
return services.counter.count;
});
const Counter = <template>
{{count}} <button type="button" {{on "click" (inc)}}>+</button>
</template>
export default Counter;
```
```ts [services/counter.ts]
import { tracked } from '@glimmer/tracking';
import Service from '@ember/service';
export default class CounterService extends Service {
@tracked count = 0;
inc = () => {
this.count++;
}
dec = () => {
this.count--;
}
}
declare module '@ember/service' {
export interface Registry {
counter: CounterService;
}
}
```
:::
Due to Ember's helper infrastructure, an `action()` returns a factory, which in
the template must be invoked, so Ember can associate the action with the helper
manager that can find the owner. Thus, the *following code will break* as Ember
is not able to make that association.
```hbs
{{on "click" inc}}
```
## Connect Business Logic
Here is the request offer function for an addition to super-rentals example. The
`requestOffer()` function contains the chunk of business logic (suprisingly these
are most often only a few lines of code). The `requestOffer()` function can be
properly unit tested to ensure it will find the right way into the backend with
the expected payload.
```ts
type Expose = object; // typed somewhere else
export interface DataClient {
sendCommand(na…