Skip to content

Commit 09b1e43

Browse files
committed
update article.md -- event delegation
1 parent 17d18bf commit 09b1e43

1 file changed

Lines changed: 36 additions & 33 deletions

File tree

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

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11

22
# Event delegation
33

4-
Capturing and bubbling allow us to implement one of the most powerful event handling patterns called *event delegation*.
4+
کپچر (capture) و bubbling ایونت ها به ما این توانایی را میدهد که از یکی از قویترین الگوهای کنترل ایونت هندلینگ یعنی *event delegation* استفاده کنیم.
55

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+
ایده این است که اگر تعداد زیادی المنت داریم و میخواهیم به یک شکل آنها رو هندل کنیم به جای اینکه به تک تک آنها هندلر مجزا اختصاص دهیم، یک هندلر را برای المنت والد مشترک آنها اختصاص میدهیم.
77

8-
In the handler we get `event.target` to see where the event actually happened and handle it.
8+
در هندلری که اختصاص میدهیم با استفاده از `event.target` محل وقوع رویداد را متوجه میشویم و بنابراین میتوانیم آنرا هندل کنیم.
99

1010
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) که یک فلسفه چینی باستانی رو نشون میده
1112

12-
Here it is:
13+
به این شکل :
1314

1415
[iframe height=350 src="bagua" edit link]
1516

1617
The HTML is like this:
18+
اچ‌تی‌ام‌ال به این صورت هست:
1719

1820
```html
1921
<table>
@@ -30,15 +32,15 @@ The HTML is like this:
3032
</table>
3133
```
3234

33-
The table has 9 cells, but there could be 99 or 9999, doesn't matter.
35+
جدول 9 سلول دارد اما این عدد امکان دارد 99 یا 9999 باشد. مهم نیست.
3436

35-
**Our task is to highlight a cell `<td>` on click.**
37+
**ماموریت ما این است که سلول `<td>` که روی آن کلیک شد را هایلایت کنیم**
3638

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>` اساین میکنیم.
3840

39-
It will use `event.target` to get the clicked element and highlight it.
41+
اینکار باعث استفاده از `event.target` برای گرفتن المنت کلیک شده و هایلایت آن می‌شود.
4042

41-
The code:
43+
کد:
4244

4345
```js
4446
let selectedTd;
@@ -62,13 +64,13 @@ function highlight(td) {
6264
}
6365
```
6466

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>` رو به شکل دینامیکی هر زمان که خواستیم اضافه/کم کنیم و همچنان هایلایت کردن کار خواهد کرد.
6668

67-
Still, there's a drawback.
69+
اما همچنان یک اشکال وجود دارد.
6870

69-
The click may occur not on the `<td>`, but inside it.
71+
کلیک ممکن است نه روی `<td>` بلکه درون آن اتفاق بیفتد.
7072

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>`
7274

7375
```html
7476
<td>
@@ -79,13 +81,13 @@ In our case if we take a look inside the HTML, we can see nested tags inside `<t
7981
</td>
8082
```
8183

82-
Naturally, if a click happens on that `<strong>` then it becomes the value of `event.target`.
84+
طبیعتا زمانی که یک کلیک بر روی `<strong>` انجام میشود آنگاه مقدار `event.target` برابر آن خواهد شد.
8385

8486
![](bagua-bubble.svg)
8587

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>` اتفاق افتاده یا نه.
8789

88-
Here's the improved code:
90+
کد بهبود یافته شده:
8991

9092
```js
9193
table.onclick = function(event) {
@@ -99,27 +101,28 @@ table.onclick = function(event) {
99101
};
100102
```
101103

102-
Explanations:
104+
توضیحات:
103105
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.
104106
2. If `event.target` is not inside any `<td>`, then the call returns immediately, as there's nothing to do.
105107
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>`.
106108
4. And, if it's so, then highlight it.
107109

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>` در جدول ارتباطی ندارد.
109111

110112
## Delegation example: actions in markup
111113

112-
There are other uses for event delegation.
114+
استفاده‌های دیگری هم برای event delegation وجود دارد
113115

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` و ... وجود دارد. چطور میتوانیم این متدهارا به دکمه‌های مربوطه وصل کنیم؟
115117

116118
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` دکمه‌ها اضافه کنیم.
117120

118121
```html
119122
<button *!*data-action="save"*/!*>Click to Save</button>
120123
```
121124

122-
The handler reads the attribute and executes the method. Take a look at the working example:
125+
هندلر اتریبیوت را خوانده و متد را اجرا میکند. نگاهی به این مثال واقعی بیندازید:
123126

124127
```html autorun height=60 run untrusted
125128
<div id="menu">
@@ -244,27 +247,27 @@ We can combine multiple behaviors on a single element as well.
244247

245248
The "behavior" pattern can be an alternative to mini-fragments of JavaScript.
246249

247-
## Summary
250+
## خلاصه
248251

249-
Event delegation is really cool! It's one of the most helpful patterns for DOM events.
252+
الگوی Event delegation واقعا باحال است! این الگو یکی از مفیدترین الگوها برای ایونت‌های DOM است.
250253

251-
It's often used to add the same handling for many similar elements, but not only for that.
254+
این الگو معمولا برای کنترل تعداد زیادی از المان‌های مشابه استفاده می‌شود اما این تنها کاربرد آن نیست.
252255

253-
The algorithm:
256+
الگوریت:
254257

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. اگر ایونت درون المنتی که موردنظر ماست اتفاق افتاده است آنگاه ایونت را هندل کنید.
258261

259-
Benefits:
262+
فواید:
260263

261264
```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` و ابزارهای مشابه آن تعداد زیادی المان را کم/زیاد کنیم.
265268
```
266269

267-
The delegation has its limitations of course:
270+
بدون شک این الگو هم محدودیت‌های خودش را دارد:
268271

269272
```compare
270273
- First, the event must be bubbling. Some events do not bubble. Also, low-level handlers should not use `event.stopPropagation()`.

0 commit comments

Comments
 (0)