@@ -8,64 +8,70 @@ the context of the template:
8
8
9
9
``` javascript {data-filename=app/components/post/component.js}
10
10
import Component from ' @glimmer/component' ;
11
+ import { tracked } from ' @glimmer/tracking' ;
11
12
import { action } from ' @ember/object' ;
12
13
13
14
export default class Post extends Component {
15
+ @tracked isShowingBody;
16
+
14
17
@action
15
18
toggleBody () {
16
- this .toggleProperty ( ' isShowingBody' ) ;
19
+ this .isShowingBody = ! this . isShowingBody ;
17
20
}
18
21
}
19
22
```
20
23
21
- You can then add this action directly to an [ _ event handler
22
- property _ ] ( https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers )
23
- on an element, like ` onclick ` or ` onmouseenter ` :
24
+ You can then add this action to an element using the
25
+ [ ` {{action}} ` ] ( https://api.emberjs.com/ember/release/classes/Ember.Templates.helpers/methods/action?anchor=action )
26
+ helper :
24
27
25
28
``` handlebars {data-filename=app/components/post/template.hbs}
26
29
<h3>
27
- <button onclick={{ this.toggleBody}}>{{this. title}}</button>
30
+ <button {{action this.toggleBody}}>{{@ title}}</button>
28
31
</h3>
29
32
30
33
{{#if this.isShowingBody}}
31
- <p>{{this. body}}</p>
34
+ <p>{{@ body}}</p>
32
35
{{/if}}
33
36
```
34
37
35
- This assigns the action to the standard browser event handler for that function.
36
- It'll receive the event as its first parameter, and you can handle it like any
37
- standard JavaScript event:
38
-
39
- ``` javascript {data-filename=app/components/post/component.js}
40
- import Component from ' @glimmer/component' ;
41
- import { action } from ' @ember/object' ;
42
-
43
- export default class Post extends Component {
44
- @action
45
- toggleBody (event ) {
46
- event .preventDefault ();
47
- event .stopPropagation ();
48
- this .toggleProperty (' isShowingBody' );
49
- }
50
- }
51
- ```
52
-
38
+ The ` {{action}} ` helper calls your action function when the element is
39
+ clicked.
53
40
You will learn about more advanced usages in the Component's [ Actions and
54
41
Events] ( ../../components/actions-and-events/ ) guide, but you should familiarize
55
- yourself with the following basics first.
42
+ yourself with the basics on this page first.
43
+
44
+ Templates rendered for your application's routes are backed by controllers, so
45
+ you may also see actions defined on a controller using the same ` @action `
46
+ decorator.
47
+
48
+ <div class =" cta " >
49
+ <div class =" cta-note " >
50
+ <div class="cta-note-body">
51
+ <div class="cta-note-heading">Zoey says...</div>
52
+ <div class="cta-note-message">
53
+ If this doesn't seem familiar you might be looking for documentation
54
+ about Ember components and controller that use the older <code >.extend({</code > syntax.
55
+ In those files you define actions in an object on the class then reference
56
+ the action name with a string in the template.
57
+ For more examples of that syntax see <a href =" https://guides.emberjs.com/v3.6.0/templates/actions/ " >previous version of this Guide entry</a >.
58
+ </div >
59
+ </div >
60
+ <img src =" /images/mascots/zoey.png " role =" presentation " alt =" Ember Mascot " >
61
+ </div >
62
+ </div >
56
63
57
64
## Action Parameters
58
65
59
- You can optionally pass arguments to the action with the
60
- [ ` {{action}} ` ] ( https://www.emberjs.com/api/ember/release/classes/Ember.Templates.helpers/methods/action?anchor=action )
66
+ You can optionally pass arguments to the action
61
67
helper. For example, if the ` post ` argument was passed:
62
68
63
69
``` handlebars {data-filename=app/components/post/template.hbs}
64
70
<p>
65
- <button onclick= {{action this.select this. post}}>
71
+ <button {{action this.select @ post}}>
66
72
✓
67
73
</button>
68
- {{this.post .title}}
74
+ {{this.selectedPost .title}}
69
75
</p>
70
76
```
71
77
@@ -87,71 +93,37 @@ export default class Post extends Component {
87
93
}
88
94
```
89
95
90
- If you pass arguments like this, the event will be the _ last_ argument that is
91
- passed to the handler:
96
+ ## Attaching Actions to Other Events
97
+
98
+ Actions don't need to be triggered on click, in fact they can be attached
99
+ to any event Ember is already listening to. For example this form will
100
+ call the ` updateText ` action when submitted, but prevent the default
101
+ form submission logic in the browser from running:
102
+
103
+ ``` handlebars {data-filename=app/components/post/template.hbs}
104
+ <form {{action this.createPost on="submit" preventDefault=true}}>
105
+ Post title: <Input @value={{this.title}} />
106
+ </form>
107
+ ```
92
108
93
109
``` javascript {data-filename=app/components/post/component.js}
94
110
import Component from ' @glimmer/component' ;
95
111
import { tracked } from ' @glimmer/tracking' ;
96
112
import { action } from ' @ember/object' ;
97
113
98
114
export default class Post extends Component {
99
- @tracked selectedPost ;
115
+ @tracked title ;
100
116
101
117
@action
102
- select (post , event ) {
103
- event .preventDefault ();
104
- event .stopPropagation ();
105
- this .selectedPost = post;
106
- }
107
- }
108
- ```
109
-
110
- ## Modifying the action's first parameter
111
-
112
- If a ` value ` option for the ` {{action}} ` helper is specified, its value will be
113
- considered a property path that will be read off of the first parameter of the
114
- action. This comes very handy with event listeners and enables to work with
115
- one-way bindings.
116
-
117
- ``` handlebars
118
- <label>What's your favorite band?</label>
119
- <input
120
- type="text"
121
- value={{this.favoriteBand}}
122
- onblur={{this.bandDidChange}}
123
- />
124
- ```
125
-
126
- Let's assume we have an action handler that prints its first parameter:
127
-
128
- ``` javascript
129
- actions: {
130
- bandDidChange (newValue ) {
131
- console .log (newValue);
118
+ createPost () {
119
+ // Do something with `this.title`
132
120
}
133
121
}
134
122
```
135
123
136
- By default, the action handler receives the first parameter of the event
137
- listener, the event object the browser passes to the handler, so ` bandDidChange `
138
- prints ` Event {} ` .
139
-
140
- Using the ` value ` option modifies that behavior by extracting that property from
141
- the event object:
142
-
143
- ``` handlebars
144
- <label>What's your favorite band?</label>
145
- <input
146
- type="text"
147
- value={{this.favoriteBand}}
148
- onblur={{action this.bandDidChange value="target.value"}}
149
- />
150
- ```
151
-
152
- The ` newValue ` parameter thus becomes the ` target.value ` property of the event
153
- object, which is the value of the input field the user typed. (e.g 'Foo
154
- Fighters')
124
+ Read about which events Ember is already listening to in
125
+ [ Handling Events: Event
126
+ Names] ( ../../components/handling-events/#toc_event-names ) .
155
127
156
128
## Attaching Actions to Non-Clickable Elements
157
129
0 commit comments