Skip to content

Commit 1f8c611

Browse files
pzuraqjenweber
authored andcommitted
[OCTANE] Fixup Actions and Events page/links (ember-learn#618)
1 parent 550f9c0 commit 1f8c611

File tree

3 files changed

+23
-44
lines changed

3 files changed

+23
-44
lines changed

guides/release/components/actions-and-events.md

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ Like we mentioned at the beginning of this section, components are like custom,
22
user-defined HTML elements. So far we've seen how to pass argument values
33
into components, which is similar to passing attributes into HTML elements like
44
`input`s, `select`s, or `dialog`s and how components can use these argument values
5-
in both their JavaScript and template. We've also seen how you can pass
6-
actual HTML attributes forward, allowing you to customize the elements that
5+
in both their JavaScript and template. We've also seen how you can pass
6+
actual HTML attributes forward, allowing you to customize the elements that
77
components produce.
88

99
But how do we make these components interactive? How do we respond to user
@@ -76,14 +76,11 @@ export default class ButtonWithConfirmation extends Component {
7676

7777
Like we discussed [Templating](../../templates/actions/), actions are methods
7878
that are decorated with the `@action` decorator, and which can be used in
79-
templates. We can assign the action to our button's
80-
[`onclick`](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onclick)
81-
event:
79+
templates. We can assign the action to our button using the `{{action}}`
80+
modifier:
8281

8382
```handlebars
84-
<button
85-
onclick={{this.launchConfirmationDialog}}
86-
>
83+
<button {{action this.launchConfirmationDialog}}>
8784
{{@text}}
8885
</button>
8986
@@ -129,23 +126,21 @@ export default class ButtonWithConfirmation extends Component {
129126
```
130127

131128
```handlebars
132-
<button
133-
onclick={{this.launchConfirmationDialog}}
134-
>
129+
<button {{action this.launchConfirmationDialog}}>
135130
{{@text}}
136131
</button>
137132
138133
{{#if this.showConfirmation}}
139134
<div class="confirm-dialog">
140135
<button
141136
class="confirm-submit"
142-
onclick={{this.submitConfirm}}
137+
{{action this.submitConfirm}}
143138
>
144139
OK
145140
</button>
146141
<button
147142
class="confirm-cancel"
148-
onclick={{this.cancelConfirm}}
143+
{{action this.cancelConfirm}}
149144
>
150145
Cancel
151146
</button>
@@ -395,13 +390,13 @@ be used in block form and we will [yield](../yields/) `confirmValue` to the
395390
block within the `"confirmDialog"` element:
396391

397392
```handlebars {data-filename=app/templates/components/button-with-confirmation.hbs}
398-
<button type="button" onclick={{this.launchConfirmDialog}}>{{this.text}}</button>
393+
<button type="button" {{action this.launchConfirmDialog}}>{{this.text}}</button>
399394
400395
{{#if this.confirmShown}}
401396
<div class="confirm-dialog">
402397
{{yield this.confirmValue}}
403-
<button class="confirm-submit" type="button" onclick={{this.submitConfirm}}>OK</button>
404-
<button class="confirm-cancel" type="button" onclick={{this.cancelConfirm}}>Cancel</button>
398+
<button class="confirm-submit" type="button" {{action this.submitConfirm}}>OK</button>
399+
<button class="confirm-cancel" type="button" {{action this.cancelConfirm}}>Cancel</button>
405400
</div>
406401
{{/if}}
407402
```
@@ -526,7 +521,7 @@ adding JavaScript code to those child components.
526521
For example, say we want to move account deletion from the `UserProfile`
527522
component to its parent `system-preferences-editor`.
528523

529-
First we would move the `deleteUser` action from `user-profile.js` to
524+
First we would move the `deleteUser` action from `user-profile.js` to
530525
the parent `system-preferences-editor.js`.
531526

532527
```javascript {data-filename=app/components/system-preferences-editor.js}
@@ -575,32 +570,17 @@ Now when you confirm deletion, the action goes straight to the
575570

576571
## String Action Syntax
577572

578-
Historically the `{{action}}` helper (`<button onclick={{action 'clickedButton'}}>`)
573+
Historically the `{{action}}` helper (`<MyComponent @onClick=(action 'clickedButton')>`)
579574
and element modifier (`<button {{action 'clickedButton'}}>`) have accepted a
580575
string as the first argument. This form is no longer recommended, but might be
581576
seen in the wild when working on older Ember apps or addons.
582577

583578
When you encounter a string based action it should be refactored to use the
584579
`@action` decorator (refactor away from the `actions` hash).
585580

586-
It is then recommended to use HTML's
587-
[on...](https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Event_handlers)
588-
attributes in the template.
581+
It is then recommended to use the action modifier directly with decorated
582+
functions.
589583

590584
```hbs
591-
<button onclick={{this.confirmDelete}}>Confirm Delete</button>
592-
```
593-
594-
If you need to add additional arguments, you can use the `{{action}}` helper.
595-
596-
```hbs
597-
<button onclick={{action this.confirmDelete this.user}}>Confirm Delete</button>
598-
```
599-
600-
If you use the action as an element modifier, you need to use the `{{action}}`
601-
element modifier in all cases. This works the same even if you don't need
602-
additional arguments.
603-
604-
```hbs
605-
<button {{action this.confirmDelete}}>Confirm Delete</button>
585+
<button {{action this.confirmDelete this.user}}>Confirm Delete</button>
606586
```

guides/release/pages.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
url: "defining-a-component"
5858
- title: "Arguments and Attributes"
5959
url: "arguments-and-attributes"
60-
- title: "Actions"
61-
url: "actions"
60+
- title: "Actions and Events"
61+
url: "actions-and-events"
6262
- title: "Yields"
6363
url: "yields"
6464
- title: "Interacting with the DOM"

guides/release/templates/actions.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default class Post extends Component {
2323

2424
You can then add this action to an element using the
2525
[`{{action}}`](https://api.emberjs.com/ember/release/classes/Ember.Templates.helpers/methods/action?anchor=action)
26-
helper:
26+
modifier:
2727

2828
```handlebars {data-filename=app/components/post/template.hbs}
2929
<h3>
@@ -35,11 +35,10 @@ helper:
3535
{{/if}}
3636
```
3737

38-
The `{{action}}` helper calls your action function when the element is
39-
clicked.
40-
You will learn about more advanced usages in the Component's [Actions and
41-
Events](../../components/actions-and-events/) guide, but you should familiarize
42-
yourself with the basics on this page first.
38+
The `{{action}}` modifier calls your action function when the element is
39+
clicked. You will learn about more advanced usages in the Component's [Actions
40+
and Events](../../components/actions-and-events/) guide, but you should
41+
familiarize yourself with the basics on this page first.
4342

4443
Templates rendered for your application's routes are backed by controllers, so
4544
you may also see actions defined on a controller using the same `@action`

0 commit comments

Comments
 (0)