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
+22-22
Original file line number
Diff line number
Diff line change
@@ -107,25 +107,25 @@ Penjelasan:
107
107
108
108
Hasilnya, kita memiliki kode yang cepat, efisien dalam memberikan highlight, yang tidak peduli terhadap jumlah dari elemen `<td>` pada sebuah tabel.
109
109
110
-
## Delegation example: actions in markup
110
+
## Contoh Delegasi: tindakan dalam markup
111
111
112
-
There are other uses for event delegation.
112
+
Ada kegunaan lain untuk delegasi acara.
113
113
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?
114
+
Bayangkan, kita mau membuat sebuah menu dengan tombol "Simpan", "Muat", "Cari" dan seterusnya. Dan ada sebuah objek dengan metode `simpan`, `muat`, `cari`... Bagaimana cara untuk menyamakan mereka?
115
115
116
-
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:
116
+
Ide pertama yaitu dengan mengatur penangan (_handler_) berbeda pada setiap tombol, Tapi ada solusi yang lebih elegan. Kita bisa menambahkan sebuah penangan (_handler_) untuk seseluruhan menu dan menambahkan atribut `data-action`untuk tombol yang bisa memanggil/memiliki sebuah metode:
117
117
118
118
```html
119
-
<button*!*data-action="save"*/!*>Click to Save</button>
119
+
<button*!*data-action="save"*/!*>Klik untuk simpan</button>
120
120
```
121
121
122
-
The handler reads the attribute and executes the method. Take a look at the working example:
122
+
Penangan (_handler_) membaca atribut dan mengeksekusi metode yang sama dengan atribut. Coba lihat contohnya:
123
123
124
124
```html autorun height=60 run untrusted
125
125
<divid="menu">
126
-
<buttondata-action="save">Save</button>
127
-
<buttondata-action="load">Load</button>
128
-
<buttondata-action="search">Search</button>
126
+
<buttondata-action="save">Simpan</button>
127
+
<buttondata-action="load">Muat</button>
128
+
<buttondata-action="search">Cari</button>
129
129
</div>
130
130
131
131
<script>
@@ -136,15 +136,15 @@ The handler reads the attribute and executes the method. Take a look at the work
136
136
}
137
137
138
138
save() {
139
-
alert('saving');
139
+
alert('Menyimpan');
140
140
}
141
141
142
142
load() {
143
-
alert('loading');
143
+
alert('Memuat');
144
144
}
145
145
146
146
search() {
147
-
alert('searching');
147
+
alert('Mencari');
148
148
}
149
149
150
150
onClick(event) {
@@ -161,24 +161,24 @@ The handler reads the attribute and executes the method. Take a look at the work
161
161
</script>
162
162
```
163
163
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
+
Harap dicatat bahwa`this.onClick`terikat pada `this`di`(*)`. Itu penting, karena jika tidak `this`didalamnya akan menyimpan referensi ke DOM elemen (`elem`), buka ke objek `Menu`, dan`this[action]`tidak akan seperti yang kita inginkan.
165
165
166
-
So, what advantages does delegation give us here?
166
+
Jadi,apakah keuntuk yang diberikan delegasi kepada kita disini?
167
167
168
168
```compare
169
-
+ We don't need to write the code to assign a handler to each button. Just make a method and put it in the markup.
170
-
+ The HTML structure is flexible, we can add/remove buttons at any time.
169
+
+ Kita teidak perlu lagi menulis kode untuk mengatur penangan (_handler_) untuk setiap tombol. Kita hanya perlu membuat sebuah metode dan menaruh markup didalamnya.
170
+
+ Struktur HTML menjadi fleksible, dan kita bisa menambah/menghapus tombol kapanpun kita mau.
171
171
```
172
172
173
-
We could also use classes`.action-save`, `.action-load`, but an attribute`data-action`is better semantically. And we can use it in CSS rules too.
173
+
Kita juga bisa menggunakan _class_`.action-save`, `action-load`, tapi sebuah atribut`data-action`lebih baik secara semantik. Dan kita bisa gunakan itu pada aturan CSS juga.
174
174
175
-
## The "behavior" pattern
175
+
## Perilaku pola
176
176
177
-
We can also use event delegation to add "behaviors" to elements *declaratively*, with special attributes and classes.
177
+
Kita juga bisa menggunakan delegasi peristiwa untuk menambahkan 'perilaku' kepada elemen secara deklarasi, dengan atribut khusus dan _class_.
178
178
179
-
The pattern has two parts:
180
-
1.We add a custom attribute to an element that describes its behavior.
181
-
2.A document-wide handler tracks events, and if an event happens on an attributed element -- performs the action.
179
+
Pola memiliki 2 bagian:
180
+
1.Kita tambahkan sebuah atribut khusus ke sebuah elemen yang menjelaskan perilakunya.
181
+
2.Penangan dokumen secara umum untuk melacak peristiwa, dan jika sebuah peristiwa terjadi pada elemen yang memiliki atribut khusus -- jalankan sebuah proses.
0 commit comments