You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/2-events/03-event-delegation/article.md
+36-33Lines changed: 36 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,21 @@
1
1
2
2
# Event delegation
3
3
4
-
Capturing and bubbling allow us to implement one of the most powerful event handling patterns called *event delegation*.
4
+
کپچر (capture) و bubbling ایونت ها به ما این توانایی را میدهد که از یکی از قویترین الگوهای کنترل ایونت هندلینگ یعنی *event delegation* استفاده کنیم.
5
5
6
-
The idea is that if we have a lot of elements handled in a similar way, then instead of assigning a handler to each of them -- we put a single handler on their common ancestor.
6
+
ایده این است که اگر تعداد زیادی المنت داریم و میخواهیم به یک شکل آنها رو هندل کنیم به جای اینکه به تک تک آنها هندلر مجزا اختصاص دهیم، یک هندلر را برای المنت والد مشترک آنها اختصاص میدهیم.
7
7
8
-
In the handler we get `event.target`to see where the event actually happened and handle it.
8
+
در هندلری که اختصاص میدهیم با استفاده از `event.target`محل وقوع رویداد را متوجه میشویم و بنابراین میتوانیم آنرا هندل کنیم.
9
9
10
10
Let's see an example -- the [Ba-Gua diagram](http://en.wikipedia.org/wiki/Ba_gua) reflecting the ancient Chinese philosophy.
11
+
بیاید تا با هم یک مثال رو بررسی کنیم -- [دیاگرام Ba-Gua] (http://en.wikipedia.org/wiki/Ba_gua) که یک فلسفه چینی باستانی رو نشون میده
11
12
12
-
Here it is:
13
+
به این شکل :
13
14
14
15
[iframe height=350 src="bagua" edit link]
15
16
16
17
The HTML is like this:
18
+
اچتیامال به این صورت هست:
17
19
18
20
```html
19
21
<table>
@@ -30,15 +32,15 @@ The HTML is like this:
30
32
</table>
31
33
```
32
34
33
-
The table has 9 cells, but there could be 99 or 9999, doesn't matter.
35
+
جدول 9 سلول دارد اما این عدد امکان دارد 99 یا 9999 باشد. مهم نیست.
34
36
35
-
**Our task is to highlight a cell `<td>`on click.**
37
+
**ماموریت ما این است که سلول `<td>`که روی آن کلیک شد را هایلایت کنیم**
36
38
37
-
Instead of assign an`onclick`handler to each `<td>`(can be many) -- we'll setup the "catch-all" handler on `<table>`element.
39
+
به جای آنکه هندلر`onclick`را به هریک از تگ های `<td>`اساین کنیم (که ممکن است تعداد زیادی از آنها داشته باشیم)، هندلر "catch-all" را برروی المنت `<table>`اساین میکنیم.
38
40
39
-
It will use `event.target`to get the clicked element and highlight it.
41
+
اینکار باعث استفاده از `event.target`برای گرفتن المنت کلیک شده و هایلایت آن میشود.
40
42
41
-
The code:
43
+
کد:
42
44
43
45
```js
44
46
let selectedTd;
@@ -62,13 +64,13 @@ function highlight(td) {
62
64
}
63
65
```
64
66
65
-
Such a code doesn't care how many cells there are in the table. We can add/remove `<td>`dynamically at any time and the highlighting will still work.
67
+
این کد به اینکه چند سلول داخل جدول قرار دارد اهمیتی نمیدهد. میتوانیم المنتهای `<td>`رو به شکل دینامیکی هر زمان که خواستیم اضافه/کم کنیم و همچنان هایلایت کردن کار خواهد کرد.
66
68
67
-
Still, there's a drawback.
69
+
اما همچنان یک اشکال وجود دارد.
68
70
69
-
The click may occur not on the `<td>`, but inside it.
71
+
کلیک ممکن است نه روی `<td>` بلکه درون آن اتفاق بیفتد.
70
72
71
-
In our case if we take a look inside the HTML, we can see nested tags inside`<td>`, like `<strong>`:
73
+
در اینصورت اگر به داخل HTML نگاهی بندازیم میتوانیم تگ های درون`<td>` را ببینمی. مثل المنت `<strong>`
72
74
73
75
```html
74
76
<td>
@@ -79,13 +81,13 @@ In our case if we take a look inside the HTML, we can see nested tags inside `<t
79
81
</td>
80
82
```
81
83
82
-
Naturally, if a click happens on that`<strong>`then it becomes the value of `event.target`.
84
+
طبیعتا زمانی که یک کلیک بر روی`<strong>`انجام میشود آنگاه مقدار `event.target` برابر آن خواهد شد.
83
85
84
86

85
87
86
-
In the handler `table.onclick`we should take such `event.target`and find out whether the click was inside `<td>`or not.
88
+
در هندلر `table.onclick`ما باید مقدار `event.target`را گرفته و از این طریق مشخص کنیم که آیا کلیک درون `<td>`اتفاق افتاده یا نه.
1. The method `elem.closest(selector)` returns the nearest ancestor that matches the selector. In our case we look for `<td>` on the way up from the source element.
104
106
2. If `event.target` is not inside any `<td>`, then the call returns immediately, as there's nothing to do.
105
107
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>`.
106
108
4. And, if it's so, then highlight it.
107
109
108
-
As the result, we have a fast, efficient highlighting code, that doesn't care about the total number of `<td>`in the table.
110
+
در نتیحه ما یک کد هایلایتر سریع و کارا داریم که عملکرد آن به تعداد سلولهای `<td>`در جدول ارتباطی ندارد.
109
111
110
112
## Delegation example: actions in markup
111
113
112
-
There are other uses for event delegation.
114
+
استفادههای دیگری هم برای event delegation وجود دارد
113
115
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?
116
+
در نظر بگیرید که میخواهیم یک منو با دکمههای "Save", "Load"، "Search" و مانند آن بسازیم آبجکتی با متدهای `save`, `load`, `search` و ... وجود دارد. چطور میتوانیم این متدهارا به دکمههای مربوطه وصل کنیم؟
115
117
116
118
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:
119
+
اولین ایدهای که به ذهن میرسد ممکن است این باشد که برای هریک از دکمهها هندلر مجزا اساین کنیم. اما راهحل بهتری هم وجود دارد. میتوانیم یک هندلر به کل منو و اتریبیوتهای `data-action` دکمهها اضافه کنیم.
117
120
118
121
```html
119
122
<button*!*data-action="save"*/!*>Click to Save</button>
120
123
```
121
124
122
-
The handler reads the attribute and executes the method. Take a look at the working example:
125
+
هندلر اتریبیوت را خوانده و متد را اجرا میکند. نگاهی به این مثال واقعی بیندازید:
123
126
124
127
```html autorun height=60 run untrusted
125
128
<divid="menu">
@@ -244,27 +247,27 @@ We can combine multiple behaviors on a single element as well.
244
247
245
248
The "behavior" pattern can be an alternative to mini-fragments of JavaScript.
246
249
247
-
## Summary
250
+
## خلاصه
248
251
249
-
Event delegation is really cool! It's one of the most helpful patterns for DOM events.
252
+
الگوی Event delegation واقعا باحال است! این الگو یکی از مفیدترین الگوها برای ایونتهای DOM است.
250
253
251
-
It's often used to add the same handling for many similar elements, but not only for that.
254
+
این الگو معمولا برای کنترل تعداد زیادی از المانهای مشابه استفاده میشود اما این تنها کاربرد آن نیست.
252
255
253
-
The algorithm:
256
+
الگوریت:
254
257
255
-
1.Put a single handler on the container.
256
-
2.In the handler -- check the source element `event.target`.
257
-
3. If the event happened inside an element that interests us, then handle the event.
258
+
1.یک هندلر برای کل کانتینر قرار دهید.
259
+
2.در هندلر المنت سورس را از طریق `event.target` بیابید.
260
+
4. اگر ایونت درون المنتی که موردنظر ماست اتفاق افتاده است آنگاه ایونت را هندل کنید.
258
261
259
-
Benefits:
262
+
فواید:
260
263
261
264
```compare
262
-
+ Simplifies initialization and saves memory: no need to add many handlers.
263
-
+ Less code: when adding or removing elements, no need to add/remove handlers.
264
-
+ DOM modifications: we can mass add/remove elements with `innerHTML` and the like.
265
+
+ عدم نیاز به اضافه کردن تعداد زیادی هندلر باعث ذخیره حافظه و سادهسازی شروع میشود
266
+
+ کد کمتر: زمان اضافه و کم کردن المانها نیازی به تغییر در هندلرها نیست.
267
+
+ تغییرات DOM: میتواینم با استفاده از `innerHTML` و ابزارهای مشابه آن تعداد زیادی المان را کم/زیاد کنیم.
265
268
```
266
269
267
-
The delegation has its limitations of course:
270
+
بدون شک این الگو هم محدودیتهای خودش را دارد:
268
271
269
272
```compare
270
273
- First, the event must be bubbling. Some events do not bubble. Also, low-level handlers should not use `event.stopPropagation()`.
0 commit comments