Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecation guide for (action) / {{action}} #1377

Merged
merged 7 commits into from
May 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Deprecation guide for emberjs/rfcs#1006
  • Loading branch information
NullVoxPopuli committed Mar 5, 2024
commit 39ca9bcd849e853dc770d18ff492a445b8ad397d
138 changes: 138 additions & 0 deletions content/ember/v5/deprecate-template-action.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
---
title: Action helper and modifier
until: 6.0.0
since: 5.10.0
IgnaceMaes marked this conversation as resolved.
Show resolved Hide resolved
---

### Scenario: `action` is passed a string

Before:
```hbs
<button type="button" {{action "plusOne"}}>
Click Me
</button>
```

After

```hbs
<button type="button" {{on 'click' this.plusOne}}>
Click Me
</button>
```
or, if `plusOne` is passed in as an argument
```hbs
<button type="button" {{on 'click' @plusOne}}>
Click Me
</button>
```

If the `plusOne` action is in an actions object, it needs to move out:
NullVoxPopuli marked this conversation as resolved.
Show resolved Hide resolved

Before:
```js
import Component from '@glimmer/component';

export default class Demo extends Component {
actions = {
plusOne() {
/* ... */
}
}
}
```
or
```js
import Component from '@ember/component';

export default class Demo extends Component {
actions = {
plusOne() {
/* ... */
}
}
}
```
or
```js
import Component from '@ember/component';

export default Component.extend({
actions: {
plusOne() {
/* ... */
}
}
})
```

After:
```js
import Component from '@glimmer/component';
import { action } from '@ember/object';

export default class Demo extends Component {
@action
plusOne() {
/* ... */
}
}
```

Note that `@action` is completely different from `(action)` or `{{action}}` (and is partly a motivator for deprecating `(action)` and `{{action}}`, to reduce ambiguity).

`@action` is binds the `this` on the method to the instance of the class.
NullVoxPopuli marked this conversation as resolved.
Show resolved Hide resolved

### Scenario: `action` is passed a function reference

Before:
```hbs
<SomeComponent @update={{action this.plusOne}} />
```

After

```hbs
<SomeComponent @update={{this.plusOne}} />
```

### Scenario: `action` is passed parameters

Before:
```hbs
<SomeComponent @update={{action this.plus 1}} />
```

After:
```hbs
<SomeComponent @update={{fn this.plus 1}} />
```

### Scenario: `action` is used with `mut`

Before:
```hbs
<SomeComponent @update={{action (mut @value.property}} />
```
After:
```js
// parent.js
import Component from '@glimmer/component';
import { action } from '@ember/object';

export default class SomeComponent extends Component {
@action
handleUpdate(value) {
this.args.property = value;
}
}
```
```hbs
{{! parent.hbs }}
<SomeComponent @update={{this.handleUpdate}} />
```

Related, [Combining function arguments with action functions](https://guides.emberjs.com/release/components/component-state-and-actions/#toc_combining-arguments-and-actions)


For more background, read the [RFC](https://github.com/emberjs/rfcs/pull/1006)
Loading