Skip to content

Commit 1ffcb4e

Browse files
authored
Merge pull request #348 from odsantos/update-en-event-delegation
Update "Event delegation" files
2 parents 98ea21c + 025ba67 commit 1ffcb4e

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

2-ui/2-events/03-event-delegation/4-behavior-tooltip/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Should work like this:
1818

1919
[iframe src="solution" height=200 border=1]
2020

21-
In this task we assume that all elements with `data-tooltip` have only text inside. No nested tags.
21+
In this task we assume that all elements with `data-tooltip` have only text inside. No nested tags (yet).
2222

2323
Details:
2424

@@ -35,4 +35,4 @@ Please use event delegation: set up two handlers on `document` to track all "ove
3535

3636
After the behavior is implemented, even people unfamiliar with JavaScript can add annotated elements.
3737

38-
P.S. To keep things natural and simple: only one tooltip may show up at a time.
38+
P.S. Only one tooltip may show up at a time.

2-ui/2-events/03-event-delegation/article.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ Explanations:
105105
3. In case of nested tables, `event.target` may be a `<td>`, but lying outside of the current table. So we check if that's actually *our table's* `<td>`.
106106
4. And, if it's so, then highlight it.
107107

108-
## Delegation example: actions in markup
108+
As the result, we have a fast, efficient highlighting code, that doesn't care about the total number of `<td>` in the table.
109109

110-
The event delegation may be used to optimize event handling. We use a single handler for similar actions on many elements. Like we did it for highlighting `<td>`.
110+
## Delegation example: actions in markup
111111

112-
But we can also use a single handler as an entry point for many different things.
112+
There are other uses for event delegation.
113113

114-
For instance, we want to make a menu with buttons "Save", "Load", "Search" and so on. And there's an object with methods `save`, `load`, `search`....
114+
Let's say, we want to make a menu with buttons "Save", "Load", "Search" and so on. And there's an object with methods `save`, `load`, `search`... How to match them?
115115

116116
The first idea may be to assign a separate handler to each button. But there's a more elegant solution. We can add a handler for the whole menu and `data-action` attributes for buttons that has the method to call:
117117

@@ -161,7 +161,7 @@ The handler reads the attribute and executes the method. Take a look at the work
161161
</script>
162162
```
163163

164-
Please note that `this.onClick` is bound to `this` in `(*)`. That's important, because otherwise `this` inside it would reference the DOM element (`elem`), not the menu object, and `this[action]` would not be what we need.
164+
Please note that `this.onClick` is bound to `this` in `(*)`. That's important, because otherwise `this` inside it would reference the DOM element (`elem`), not the `Menu` object, and `this[action]` would not be what we need.
165165

166166
So, what advantages does delegation give us here?
167167

@@ -177,12 +177,12 @@ We could also use classes `.action-save`, `.action-load`, but an attribute `data
177177
We can also use event delegation to add "behaviors" to elements *declaratively*, with special attributes and classes.
178178

179179
The pattern has two parts:
180-
1. We add a special attribute to an element.
180+
1. We add a custom attribute to an element that describes its behavior.
181181
2. A document-wide handler tracks events, and if an event happens on an attributed element -- performs the action.
182182

183-
### Counter
183+
### Behavior: Counter
184184

185-
For instance, here the attribute `data-counter` adds a behavior: "increase on click" to buttons:
185+
For instance, here the attribute `data-counter` adds a behavior: "increase value on click" to buttons:
186186

187187
```html run autorun height=60
188188
Counter: <input type="button" value="1" data-counter>
@@ -204,14 +204,14 @@ If we click a button -- its value is increased. Not buttons, but the general app
204204
There can be as many attributes with `data-counter` as we want. We can add new ones to HTML at any moment. Using the event delegation we "extended" HTML, added an attribute that describes a new behavior.
205205

206206
```warn header="For document-level handlers -- always `addEventListener`"
207-
When we assign an event handler to the `document` object, we should always use `addEventListener`, not `document.onclick`, because the latter will cause conflicts: new handlers overwrite old ones.
207+
When we assign an event handler to the `document` object, we should always use `addEventListener`, not `document.on<event>`, because the latter will cause conflicts: new handlers overwrite old ones.
208208

209209
For real projects it's normal that there are many handlers on `document` set by different parts of the code.
210210
```
211211
212-
### Toggler
212+
### Behavior: Toggler
213213
214-
One more example. A click on an element with the attribute `data-toggle-id` will show/hide the element with the given `id`:
214+
One more example of behavior. A click on an element with the attribute `data-toggle-id` will show/hide the element with the given `id`:
215215
216216
```html autorun run height=60
217217
<button *!*data-toggle-id="subscribe-mail"*/!*>

0 commit comments

Comments
 (0)