Translation/dispatch events#162
Translation/dispatch events#162leviding merged 5 commits intojavascript-tutorial:zh-hansfrom Starriers:translation/dispatch-events
Conversation
|
@leviding 校对认领 |
|
@xutaogit ok |
xutaogit
left a comment
There was a problem hiding this comment.
校对完成。
个人的一些小建议,可能有些吹毛求疵了。
相互学习,共同进步。感谢感谢。
@Starriers @leviding
| 我们不仅可以分发事件,还可以从 JavaScript 中生成事件 | ||
|
|
||
| Custom events can be used to create "graphical components". For instance, a root element of the menu may trigger events telling what happens with the menu: `open` (menu open), `select` (an item is selected) and so on. | ||
| 自定义事件可以用于创建 "graphical components". For instance, a root element of the menu may trigger events telling what happens with the menu: `open` (menu open), `select` (an item is selected) and so on. |
There was a problem hiding this comment.
这里有可能是漏了翻译,给出我认为可行的一个方案如下:
『自定义事件可以用于创建 "graphical components". For instance, a root element of the menu may trigger events telling what happens with the menu: open (menu open), select (an item is selected) and so on.』=>『自定义事件可以用于创建 “图形组件” 。例如,菜单组件的根元素可以通过触发 open (菜单打开),select (有一项被选中)等事件告诉菜单发生了什么。』
| 自定义事件可以用于创建 "graphical components". For instance, a root element of the menu may trigger events telling what happens with the menu: `open` (menu open), `select` (an item is selected) and so on. | ||
|
|
||
| Also we can generate built-in events like `click`, `mousedown` etc, that may be good for testing. | ||
|
|
There was a problem hiding this comment.
这里好像也漏了翻译??
『Also we can generate built-in events like click, mousedown etc, that may be good for testing.』=>『我们也可以生成一些诸如 click , mousedown 等内置事件,这些会有助于测试。』
| ## 事件构造器 | ||
|
|
||
| Events form a hierarchy, just like DOM element classes. The root is the built-in [Event](http://www.w3.org/TR/dom/#event) class. | ||
| 事件形成层次结构,就像 DOM 元素类一样。根是内置的[事件](http://www.w3.org/TR/dom/#event)类。 |
| - *options* -- the object with two optional properties: | ||
| - `bubbles: true/false` -- if `true`, then the event bubbles. | ||
| - `cancelable: true/false` -- if `true`, then the "default action" may be prevented. Later we'll see what it means for custom events. | ||
| - **event type** —— 可能是像 `"click"` 或者我们自己喜欢的 `"hey-ho!"`这样的任意字符串。 |
There was a problem hiding this comment.
『event type —— 可能是像 "click" 或者我们自己喜欢的 "hey-ho!"这样的任意字符串。』=>『event type -- 可以是任何字符串,比如 "click" 或者我们自己喜欢的 "hey-ho!"。』
| - `cancelable: true/false` —— 如果 `true`,那么“默认动作”就会被阻止。之后我们会看到对于自定义事件,这些意味着什么。 | ||
|
|
||
| By default both are false: `{bubbles: false, cancelable: false}`. | ||
| 默认情况下,它们都是:`{bubbles: false, cancelable: false}`。 |
There was a problem hiding this comment.
『默认情况下,它们都是:』=>『默认情况下,它们都是 false:』
| - `cancelable: true` 则 `event.preventDefault()` 应该有效。 | ||
|
|
||
| Other constructors of native events like `MouseEvent`, `KeyboardEvent` and so on accept properties specific to that event type. For instance, `clientX` for mouse events. | ||
| 其他像 `MouseEvent`、`KeyboardEvent` 这样的原生事件构造器,接受特定于该事件类型的属性。例如,鼠标事件 `clientX`。 |
There was a problem hiding this comment.
『鼠标事件 clientX 。』=>『鼠标事件的 clientX 。』
| 其他像 `MouseEvent`、`KeyboardEvent` 这样的原生事件构造器,接受特定于该事件类型的属性。例如,鼠标事件 `clientX`。 | ||
|
|
||
| For custom events we should use `CustomEvent` constructor. It has an additional option named `detail`, we should assign the event-specific data to it. Then all handlers can access it as `event.detail`. | ||
| 对于自定义事件我们应该使用 `CustomEvent` 构造器。它有一个名为 `detail` 的附加选项,我们应该将特定于时间的数据分发给它。然后处理器可以以 `event.detail` 的形式访问它。 |
There was a problem hiding this comment.
『我们应该将特定于时间的数据分发给它。』=>『我们应该将特定事件的数据指派给它。』
|
|
||
| - As a dirty hack to make 3rd-party libraries work the needed way, if they don't provide other means of interaction. | ||
| - For automated testing, to "click the button" in the script and see if the interface reacts correctly. | ||
| - 如果第三方库不提供其他交互方式,那么让第三方库以所需的方式工作就是一种黑客行为。 |
There was a problem hiding this comment.
『如果第三方库不提供其他交互方式,那么让第三方库以所需的方式工作就是一种黑客行为。』=>『如果他们不提供其他的交互方式,脏黑客行为可以制作第三方库操作所需的方式。』
| - 对于自动化测试,要在脚本中“单击按钮”并查看接口是否正确反应。 | ||
|
|
||
| Custom events with our own names are often generated for architectural purposes, to signal what happens inside our menus, sliders, carousels etc. | ||
| 使用我们自己的名字来自定义的事件通常是为架构目的产生的,用来指示菜单、滑块、传送带等内部发送的情况。 |
There was a problem hiding this comment.
『用来指示菜单、滑块、传送带等内部发送的情况。』=>『用来指示菜单、滑块、轮播等内部发生什么。』
| @@ -1,37 +1,37 @@ | |||
| # Dispatching custom events | |||
| # 生成自定义事件 | |||
|
|
|||
|
@xutaogit 越严格越好,互相学习,共同进步 |
|
@xutaogit well down! |
| # 生成自定义事件 | ||
|
|
||
| We can not only assign handlers, but also generate events from JavaScript. | ||
| 我们不仅可以分发事件,还可以从 JavaScript 中生成事件 |
| ``` | ||
|
|
||
| The `detail` property can have any data. Technically we could live without, because we can assign any properties into a regular `new Event` object after its creation. But `CustomEvent` provides the special `detail` field for it to evade conflicts with other event properties. | ||
| `detail` 属性可以有任何数据。从技术上讲,我们可以不用,因为我们可以在创建后将任何属性分配到常规的 `new Event` 对象中。 但是 `CustomEvent` 为它提供了特殊的 `detail` 字段,以避免与其他事件属性的冲突。 |
| ## Events-in-events 同步 | ||
|
|
||
| Usually events are processed asynchronously. That is: if the browser is processing `onclick` and in the process a new event occurs, then it awaits till `onclick` processing is finished. | ||
| 事件通常都是同步处理的。也就是说:如果浏览器正在处理 `onclick`,而且在处理过程中发生了一个新的事件,那么它将等待,直到 `onclick` 处理完成。 |
#159 翻译完成