@@ -205,8 +205,8 @@ declare const SIGNATURE: unique symbol;
205
205
default for older editions of Ember (pre 3.15).
206
206
207
207
Below is the documentation for Classic components. If you are looking for the
208
- API documentation for Template-only or Glimmer components, it is
209
- [available here](/ember/release/modules/@glimmer%2Fcomponent).
208
+ API documentation for Template-only or Glimmer components, it is [available
209
+ here](/ember/release/modules/@glimmer%2Fcomponent).
210
210
211
211
## Defining a Classic Component
212
212
@@ -404,9 +404,9 @@ declare const SIGNATURE: unique symbol;
404
404
<div id="ember1" class="ember-view empty"></div>
405
405
```
406
406
407
- If you want to add a class name for a property which evaluates to true and
408
- and a different class name if it evaluates to false, you can pass a binding
409
- like this:
407
+ If you want to add a class name for a property which evaluates to true and and
408
+ a different class name if it evaluates to false, you can pass a binding like
409
+ this:
410
410
411
411
```app/components/my-widget.js
412
412
import Component from '@ember/component';
@@ -467,9 +467,9 @@ declare const SIGNATURE: unique symbol;
467
467
### Other HTML Attributes
468
468
469
469
The HTML attribute section of a component's tag can be set by providing an
470
- `attributeBindings` property set to an array of property names on the component.
471
- The return value of these properties will be used as the value of the component's
472
- HTML associated attribute:
470
+ `attributeBindings` property set to an array of property names on the
471
+ component. The return value of these properties will be used as the value of
472
+ the component's HTML associated attribute:
473
473
474
474
```app/components/my-anchor.js
475
475
import Component from '@ember/component';
@@ -488,8 +488,8 @@ declare const SIGNATURE: unique symbol;
488
488
<a id="ember1" class="ember-view" href="http://google.com"></a>
489
489
```
490
490
491
- One property can be mapped on to another by placing a ":" between
492
- the source property and the destination property:
491
+ One property can be mapped on to another by placing a ":" between the source
492
+ property and the destination property:
493
493
494
494
```app/components/my-anchor.js
495
495
import Component from '@ember/component';
@@ -522,9 +522,9 @@ declare const SIGNATURE: unique symbol;
522
522
<a id="ember1" class="ember-view" href="http://bing.com"></a>
523
523
```
524
524
525
- Note that the `href` attribute is ultimately set to `http://bing.com`,
526
- despite it having attribute binidng to the `url` property, which was
527
- set to `http://google.com`.
525
+ Note that the `href` attribute is ultimately set to `http://bing.com`, despite
526
+ it having attribute binidng to the `url` property, which was set to
527
+ `http://google.com`.
528
528
529
529
Namespaced attributes (e.g. `xlink:href`) are supported, but have to be
530
530
mapped, since `:` is not a valid character for properties in Javascript:
@@ -662,27 +662,33 @@ declare const SIGNATURE: unique symbol;
662
662
663
663
## Handling Browser Events
664
664
665
- Components can respond to user-initiated events in one of three ways: passing
666
- actions with angle bracket invocation, adding event handler methods to the
667
- component's class, or adding actions to the component's template.
665
+ There are two ways to handle user-initiated events:
668
666
669
- ### Passing Actions With Angle Bracket Invocation
667
+ ### Using the `on` modifier to capture browser events
670
668
671
- For one-off events specific to particular instance of a component, it is possible
672
- to pass actions to the component's element using angle bracket invocation syntax.
669
+ In a component's template, you can attach an event handler to any element with the `on` modifier:
673
670
674
671
```handlebars
675
- <MyWidget {{action 'firstWidgetClicked'}} />
676
-
677
- <MyWidget {{action 'secondWidgetClicked'}} />
672
+ <button {{on 'click' this.doSomething}} />
678
673
```
679
674
680
- In this case, when the first component is clicked on, Ember will invoke the
681
- `firstWidgetClicked` action. When the second component is clicked on, Ember
682
- will invoke the `secondWidgetClicked` action instead.
675
+ This will call the function on your component:
676
+
677
+ ```js
678
+ import Component from '@ember/component';
679
+
680
+ export default class ExampleComponent extends Component {
681
+ doSomething = (event) => {
682
+ // `event` is the native click Event
683
+ console.log('clicked on the button');
684
+ };
685
+ });
686
+ ```
683
687
684
- Besides `{{action}}`, it is also possible to pass any arbitrary element modifiers
685
- using the angle bracket invocation syntax.
688
+ See the [Guide on Component event
689
+ handlers](https://guides.emberjs.com/release/components/component-state-and-actions/#toc_html-modifiers-and-actions)
690
+ and the [API docs for `on`](../Ember.Templates.helpers/methods/on?anchor=on)
691
+ for more details.
686
692
687
693
### Event Handler Methods
688
694
@@ -752,43 +758,6 @@ declare const SIGNATURE: unique symbol;
752
758
* `dragEnd`
753
759
* `drop`
754
760
755
- ### `{{action}}` Helper
756
-
757
- Instead of handling all events of a particular type anywhere inside the
758
- component's element, you may instead want to limit it to a particular
759
- element in the component's template. In this case, it would be more
760
- convenient to implement an action instead.
761
-
762
- For example, you could implement the action `hello` for the `person-profile`
763
- component:
764
-
765
- ```app/components/person-profile.js
766
- import Component from '@ember/component';
767
-
768
- export default Component.extend({
769
- actions: {
770
- hello(name) {
771
- console.log("Hello", name);
772
- }
773
- }
774
- });
775
- ```
776
-
777
- And then use it in the component's template:
778
-
779
- ```app/templates/components/person-profile.hbs
780
- <h1>{{@person.name }}</h1>
781
-
782
- <button {{action 'hello' @person.name }}>
783
- Say Hello to {{@person.name }}
784
- </button>
785
- ```
786
-
787
- When the user clicks the button, Ember will invoke the `hello` action,
788
- passing in the current value of `@person.name` as an argument.
789
-
790
- See [Ember.Templates.helpers.action](/ember/release/classes/Ember.Templates.helpers/methods/action?anchor=action).
791
-
792
761
@class Component
793
762
@extends Ember.CoreView
794
763
@uses Ember.TargetActionSupport
0 commit comments