@@ -2,8 +2,8 @@ Like we mentioned at the beginning of this section, components are like custom,
2
2
user-defined HTML elements. So far we've seen how to pass argument values
3
3
into components, which is similar to passing attributes into HTML elements like
4
4
` 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
7
7
components produce.
8
8
9
9
But how do we make these components interactive? How do we respond to user
@@ -76,14 +76,11 @@ export default class ButtonWithConfirmation extends Component {
76
76
77
77
Like we discussed [ Templating] ( ../../templates/actions/ ) , actions are methods
78
78
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:
82
81
83
82
``` handlebars
84
- <button
85
- onclick={{this.launchConfirmationDialog}}
86
- >
83
+ <button {{action this.launchConfirmationDialog}}>
87
84
{{@text}}
88
85
</button>
89
86
@@ -129,23 +126,21 @@ export default class ButtonWithConfirmation extends Component {
129
126
```
130
127
131
128
``` handlebars
132
- <button
133
- onclick={{this.launchConfirmationDialog}}
134
- >
129
+ <button {{action this.launchConfirmationDialog}}>
135
130
{{@text}}
136
131
</button>
137
132
138
133
{{#if this.showConfirmation}}
139
134
<div class="confirm-dialog">
140
135
<button
141
136
class="confirm-submit"
142
- onclick={{ this.submitConfirm}}
137
+ {{action this.submitConfirm}}
143
138
>
144
139
OK
145
140
</button>
146
141
<button
147
142
class="confirm-cancel"
148
- onclick={{ this.cancelConfirm}}
143
+ {{action this.cancelConfirm}}
149
144
>
150
145
Cancel
151
146
</button>
@@ -395,13 +390,13 @@ be used in block form and we will [yield](../yields/) `confirmValue` to the
395
390
block within the ` "confirmDialog" ` element:
396
391
397
392
``` 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>
399
394
400
395
{{#if this.confirmShown}}
401
396
<div class="confirm-dialog">
402
397
{{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>
405
400
</div>
406
401
{{/if}}
407
402
```
@@ -526,7 +521,7 @@ adding JavaScript code to those child components.
526
521
For example, say we want to move account deletion from the ` UserProfile `
527
522
component to its parent ` system-preferences-editor ` .
528
523
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
530
525
the parent ` system-preferences-editor.js ` .
531
526
532
527
``` javascript {data-filename=app/components/system-preferences-editor.js}
@@ -575,32 +570,17 @@ Now when you confirm deletion, the action goes straight to the
575
570
576
571
## String Action Syntax
577
572
578
- Historically the ` {{action}} ` helper (` <button onclick={{ action 'clickedButton'}} > ` )
573
+ Historically the ` {{action}} ` helper (` <MyComponent @onClick=( action 'clickedButton') > ` )
579
574
and element modifier (` <button {{action 'clickedButton'}}> ` ) have accepted a
580
575
string as the first argument. This form is no longer recommended, but might be
581
576
seen in the wild when working on older Ember apps or addons.
582
577
583
578
When you encounter a string based action it should be refactored to use the
584
579
` @action ` decorator (refactor away from the ` actions ` hash).
585
580
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.
589
583
590
584
``` 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>
606
586
```
0 commit comments