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/99-ui-misc/01-mutation-observer/article.md
+28-26Lines changed: 28 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,15 @@
1
1
2
2
# Mutation observer
3
3
4
-
همان طور که می دانیم `MutationObserver` یک شی داخلی است که یک عنصر DOM را مشاهده می کند و هنگامی که تغییری را تشخیص می دهد یک تماس برگشتی ایجاد می کند.
4
+
همان طور که می دانیم `MutationObserver` یک inner object است که یک عنصر DOM را مشاهده می کند و هنگامی که تغییری را تشخیص می دهد یک تماس برگشتی ایجاد می کند.
5
5
6
6
ابتدا نگاهی به نحو می اندازیم و سپس یک مورد استفاده در دنیای واقعی را بررسی می کنیم تا ببینیم چنین چیزی در کجا ممکن است مفید باشد.
7
7
8
8
## Syntax
9
9
10
10
استفاده از `MutationObserver` بسیار ساده است.
11
11
12
-
اول از ههمه، یک observer با استفاده از callback-function می سازیم:
12
+
اول از همه، یک observer با استفاده از callback-function می سازیم:
-`attributeOldValue` --بود هم مقدار قذیم و هم مقدار جدید attribute به callback پاس داده می شود `true` اگر
33
33
-`characterDataOldValue` --(needs `characterData` option) پاس می دهیم، در غیر این صورت فقط مقدار جدید را callback را یه `node.data` بود هم مقدار جدید و هم مقدار قدیم `true` اگر
34
34
35
-
Then after any changes, the`callback`is executed: changes are passed in the first argument as a list of [MutationRecord](https://dom.spec.whatwg.org/#mutationrecord)objects, and the observer itself as the second argument.
35
+
سپس پس از هر تغییری،`callback`اجرا میشود: تغییرات در آرگومان اول بهعنوان فهرستی از اشیاء [MutationRecord](https://dom.spec.whatwg.org/#mutationrecord)و خود مشاهدهگر بهعنوان استدلال دوم است.
36
36
37
-
[MutationRecord](https://dom.spec.whatwg.org/#mutationrecord) objects have properties:
Everything's simple so far, right? We find code snippets in HTML and highlight them.
141
141
142
-
Now let's go on. Let's say we're going to dynamically fetch materials from a server. We'll study methods for that [later in the tutorial](info:fetch). For now it only matters that we fetch an HTML article from a webserver and display it on demand:
142
+
حالا بیایید ادامه دهیم. فرض کنید می خواهیم مطالب را به صورت پویا از یک سرور واکشی کنیم. ما روشهایی را برای آن مطالعه خواهیم کرد [later in the tutorial](info:fetch). در حال حاضر فقط مهم است که یک مقاله HTML را از یک وب سرور واکشی کنیم و آن را در صورت درخواست نمایش دهیم:
143
143
144
144
```js
145
145
let article =/* fetch new content from server */
146
146
articleElem.innerHTML= article;
147
147
```
148
148
149
-
The new `article` HTML may contain code snippets. We need to call `Prism.highlightElem` on them, otherwise they won't get highlighted.
150
149
151
-
**Where and when to call `Prism.highlightElem` for a dynamically loaded article?**
150
+
در HTML `article` جدید ممکن است حاوی کدهایی باشد. باید`Prism.highlightElem` را روی آنها صدا کنیم، در غیر این صورت برجسته نمیشوند.
151
+
152
+
**کجا و چه زمانی برای یک مقاله بارگذاری شده پویا با `Prism.highlightElem` تماس بگیرید؟**
152
153
153
-
We could append that call to the code that loads an article, like this:
154
+
میتوانیم آن فراخوان را به کدی که یک مقاله را بارگیری میکند، اضافه کنیم، مانند این:
...But, imagine if we have many places in the code where we load our content - articles, quizzes, forum posts, etc. Do we need to put the highlighting call everywhere, to highlight the code in content after loading? That's not very convenient.
166
166
167
-
And what if the content is loaded by a third-party module? For example, we have a forum written by someone else, that loads content dynamically, and we'd like to add syntax highlighting to it. No one likes patching third-party scripts.
167
+
...اما، تصور کنید اگر ما مکان های زیادی در کد داشته باشیم که در آن محتوای خود را بارگذاری می کنیم - مقالات، آزمون ها، پست های انجمن و غیره. این خیلی راحت نیست.
168
+
169
+
و اگر محتوا توسط یک ماژول شخص ثالث بارگذاری شود چه؟ به عنوان مثال، ما یک انجمن داریم که توسط شخص دیگری نوشته شده است، که محتوا را به صورت پویا بارگیری می کند، و مایلیم برجسته سازی نحوی را به آن اضافه کنیم. هیچ کس وصله اسکریپت های شخص ثالث را دوست ندارد.
168
170
169
-
Luckily, there's another option.
171
+
خوشبختانه، گزینه دیگری وجود دارد.
170
172
171
-
We can use `MutationObserver`to automatically detect when code snippets are inserted into the page and highlight them.
173
+
میتوانیم از `MutationObserver`استفاده کنیم تا بهطور خودکار زمانی که قطعههای کد در صفحه درج میشوند، شناسایی کرده و آنها را برجسته کنیم.
172
174
173
-
So we'll handle the highlighting functionality in one place, relieving us from the need to integrate it.
175
+
بنابراین ما عملکرد برجسته سازی را در یک مکان مدیریت می کنیم و ما را از نیاز به ادغام آن رها می کنیم.
174
176
175
177
### Dynamic highlight demo
176
178
177
-
Here's the working example.
179
+
و مثالی که کار می کند.
178
180
179
-
If you run this code, it starts observing the element below and highlighting any code snippets that appear there:
181
+
اگر این کد را اجرا کنید، شروع به مشاهده element زیر می کند و هر قطعه کدی را که در آنجا ظاهر می شود برجسته می کند:
180
182
181
183
```js run
182
184
let observer =newMutationObserver(mutations=> {
@@ -254,14 +256,14 @@ observer.disconnect();
254
256
```
255
257
256
258
257
-
```smart header="Records returned by `observer.takeRecords()`are removed from the processing queue"
258
-
The callback won't be called for records, returned by `observer.takeRecords()`.
259
+
``` پس smart header="سوابق برگردانده شده توسط `observer.takeRecords()`از صف پردازش حذف می شوند"
260
+
تماس برگشتی برای رکوردها که توسط 'observer.takeRecords()' برگردانده شده است، فراخوانی نمی شود.
259
261
```
260
262
261
-
```smart header="Garbage collection interaction"
262
-
Observers use weak references to nodes internally. That is, if a node is removed from the DOM, and becomes unreachable, then it can be garbage collected.
263
+
```smart header="تعامل جمع آوری زباله"
264
+
ناظران از ارجاعات ضعیف به گره ها در داخل استفاده می کنند. به این معنا که اگر یک گره از DOM حذف شود و غیرقابل دسترسی شود، میتوان زبالهها را جمعآوری کرد.
263
265
264
-
The mere fact that a DOM node is observed doesn't prevent the garbage collection.
266
+
صرف این واقعیت که یک گره DOM مشاهده می شود مانع از جمع آوری زباله نمی شود.
0 commit comments