Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate page tutorial/event #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
title: DOM events
title: Event DOM
---

As we've briefly seen already, you can listen to any event on an element with the `on:` directive:
Seperti yang sudah kita lihat, kamu bisa me *listen* sebuah event dalam elemen dengan direktif `on:`:

```html
<div on:mousemove={handleMousemove}>
The mouse position is {m.x} x {m.y}
</div>
```
```
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
title: Inline handlers
title: Handlers baris
---

You can also declare event handlers inline:
Kamu juga bisa mendeklarasikan event *handlers* dalam baris:

```html
<div on:mousemove="{e => m = { x: e.clientX, y: e.clientY }}">
The mouse position is {m.x} x {m.y}
</div>
```

The quote marks are optional, but they're helpful for syntax highlighting in some environments.
Tanda kutip bersifat opsional, tapi itu sangat membantu untuk menyoroti sintaks di lingkungan tertentu.

> In some frameworks you may see recommendations to avoid inline event handlers for performance reasons, particularly inside loops. That advice doesn't apply to Svelte — the compiler will always do the right thing, whichever form you choose.
> Pada beberapa kerangka kerja kamu mungkin melihat rekomendasi untuk menghindari penggunaan event *handlers* baris untuk alasan performa, khususnya dalam perulangan. Nasihat itu tidak berlaku untuk Svelte - kompailer selalu melakukan hal yang benar, apapun bentuk yang kamu pilih.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: Event modifiers
title: Pengubah event
---

DOM event handlers can have *modifiers* that alter their behaviour. For example, a handler with a `once` modifier will only run a single time:
DOM event *handlers* bisa memiliki *pengubah* yang mengubah perilaku dari event tersebut. Contohnya, sebuah *handler* dengan pengubah `once` akan dijalankan saat pertama kali saja:

```html
<script>
Expand All @@ -16,13 +16,13 @@ DOM event handlers can have *modifiers* that alter their behaviour. For example,
</button>
```

The full list of modifiers:
Daftar lengkap dari pengubah:

* `preventDefault` — calls `event.preventDefault()` before running the handler. Useful for client-side form handling, for example.
* `stopPropagation` — calls `event.stopPropagation()`, preventing the event reaching the next element
* `passive` — improves scrolling performance on touch/wheel events (Svelte will add it automatically where it's safe to do so)
* `capture` — fires the handler during the *capture* phase instead of the *bubbling* phase ([MDN docs](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture))
* `once` — remove the handler after the first time it runs
* `self` — only trigger handler if event.target is the element itself
* `preventDefault` — panggil `event.preventDefault()` sebelum menjalankan *handler*. Berguna untuk menangani *client-side* formulir, contohnya.
* `stopPropagation` — panggil `event.stopPropagation()`, mencegah event untuk mencapai elemen berikutnya
* `passive` - meningkatkan performa *scrolling* saat event *touch/wheel* (Svelte akan menambahkannya secara otomatis di tempat yang aman untuk melakukannya)
* `capture` — Meluncurkan *handler* selama tahap *capture* daripada tahap *bubbling* ([Dokumen MDN](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture))
* `once` — menghapus *handler* setelah pertama kali dijalankan
* `self` — picu *handler* hanya saat event.target adalah element itu sendiri

You can chain modifiers together, e.g. `on:click|once|capture={...}`.
Kamu bisa menyatukan pengubah, contoh: `on:click|once|caputre={...}`.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: Component events
title: Event komponen
---

Components can also dispatch events. To do so, they must create an event dispatcher. Update `Inner.svelte`:
Komponen juga bisa mengirim event. Untuk melakukannya, mereka harus membuat sebuah pengirim event. Perbarui `Inner.svelte`:

```html
<script>
Expand All @@ -18,4 +18,4 @@ Components can also dispatch events. To do so, they must create an event dispatc
</script>
```

> `createEventDispatcher` must be called when the component is first instantiated — you can't do it later inside e.g. a `setTimeout` callback. This links `dispatch` to the component instance.
> `createEventDispatcher` harus dipanggil saat komponen pertama kali dibuat - kamu tidak bisa melakukannya nanti contoh: dengan *callback* `setTimeout`. Ini akan menghubungkan `dispatch` ke instance komponen.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
title: Event forwarding
title: Penerusan event
---

Unlike DOM events, component events don't *bubble*. If you want to listen to an event on some deeply nested component, the intermediate components must *forward* the event.
Tidak seperti event DOM, event komponen tidak *bubble*. Jika kamu ingin *listen* ke sebuah event dalam komponen yang bersarang sangat dalam, komponen perantara harus *meneruskan* event tersebut.

In this case, we have the same `App.svelte` and `Inner.svelte` as in the [previous chapter](tutorial/component-events), but there's now an `Outer.svelte` component that contains `<Inner/>`.
Dalam hal ini, kita mempunyai `App.svelte` dan `Inner.svelte` yang sama dengan [bab sebelumnya](tutorial/component-events), tapi sekarang ada `Outer.svelte` komponen yang berisi `<Inner/>`.

One way we could solve the problem is adding `createEventDispatcher` to `Outer.svelte`, listening for the `message` event, and creating a handler for it:
Salah satu cara untuk menyelesaikan masalah ini adalah dengan menambahkan `createEventDispatcher` ke `Outer.svelte`, mendengarkan event `message`, dan membuat *handler* untuk itu:

```html
<script>
Expand All @@ -23,12 +23,12 @@ One way we could solve the problem is adding `createEventDispatcher` to `Outer.s
<Inner on:message={forward}/>
```

But that's a lot of code to write, so Svelte gives us an equivalent shorthand — an `on:message` event directive without a value means 'forward all `message` events'.
Tapi banyak kode yang harus ditulis, jadi Svelte memberikan kita sebuah steno(penulisan cepat) - dengan event direktif `on:message` tanpa sebuah nilai berarti 'meneruskan semua event `message`'.

```html
<script>
import Inner from './Inner.svelte';
</script>

<Inner on:message/>
```
```
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
title: DOM event forwarding
title: Penerusan event DOM
---

Event forwarding works for DOM events too.
Penerusan event juga bisa digunakan untuk event DOM.

We want to get notified of clicks on our `<CustomButton>` — to do that, we just need to forward `click` events on the `<button>` element in `CustomButton.svelte`:
Kita ingin diberitahu tentang event klik dalam `<CustomButton>` kita - untuk melakukan itu, kita hanya perlu meneruskan event `click` ke dalam elemen `<button>` dalam `CustomButton.svelte`:

```html
<button on:click>
Click me
</button>
```
```