update event bubbling intro.
Project: http://git-wip-us.apache.org/repos/asf/incubator-weex-site/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex-site/commit/f1ddc1ec Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex-site/tree/f1ddc1ec Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex-site/diff/f1ddc1ec Branch: refs/heads/master Commit: f1ddc1ec789abff90bc34438623a63bc53212dd3 Parents: 38839e7 Author: MrRaindrop <[email protected]> Authored: Mon Mar 26 17:57:16 2018 +0800 Committer: MrRaindrop <[email protected]> Committed: Mon Mar 26 17:57:16 2018 +0800 ---------------------------------------------------------------------- source/cn/wiki/event-bubble.md | 148 ++++++++---------------------------- source/wiki/event-bubble.md | 148 ++++++++---------------------------- 2 files changed, 66 insertions(+), 230 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex-site/blob/f1ddc1ec/source/cn/wiki/event-bubble.md ---------------------------------------------------------------------- diff --git a/source/cn/wiki/event-bubble.md b/source/cn/wiki/event-bubble.md index 964eab5..f44748a 100644 --- a/source/cn/wiki/event-bubble.md +++ b/source/cn/wiki/event-bubble.md @@ -8,142 +8,60 @@ version: 2.1 <!-- toc --> -> ä» å¨ <span class="api-version">v0.13</span> å以ä¸çæ¬ä¸æ¯æã +<span class="api-version">v0.13+</span> -Weex 2.0 å®ç°äº W3C æ åçäºä»¶å泡æºå¶ã +> **Note:** ç®åä» Weex Nativeï¼Android å iOSï¼æ¯æï¼[web 端](https://github.com/weexteam/weex-vue-render) ææ¶ä¸æ¯ææ¤é¡¹ç¹æ§. -### ä½¿ç¨ +å¦æä½ æ¯ä¸ª web å¼åè ï¼ä½ 大æ¦å¯¹æµè§å¨äºä»¶å泡æºå¶å·²ç»å¾çæäºï¼èä¸å¯è½è®¤ä¸º Weex é»è®¤ä¼æ¯æäºä»¶å泡ãç¶èï¼Weex å¨ 0.13 ä¹åæ¯ä¸æ¯æè¿ä¸ç¹æ§çï¼å¨ 0.13 çæ¬ï¼Weex æä¾äºè¿é¡¹æ¯æã -```html -<template> - <div class="root" @click="rootClick" bubble="true"> - <text style="font-size: 40px;">{{rootText}}</text> - <div class="outer" @click="parentClick"> - <div> - <text style="font-size: 40px;">{{parentText}}</text> - </div> - <text class="inner" @click="click">{{innerText}}</text> - </div> - </div> -</template> +对äºä¸çæäºä»¶å泡çå¼åè ï¼è¿éç®è¦ä»ç»ä¸ä¸æµè§å¨äºä»¶å泡æºå¶çæ¦å¿µï¼ -<script> - module.exports = { - data: { - innerText: 'click me', - parentText: '', - rootText: '' - }, - methods: { - click: function(e) { - this.innerText = 'inner bubble' - }, - parentClick: function(e) { - this.parentText = 'parent bubble' - }, - rootClick: function(e) { - this.rootText = 'root bubble' - } - } - } -</script> - -<style scoped> - .inner { - font-size: 40px; - text-align: center; - background-color: #7a9b1b; - padding: 40px; - } +## æ¦å¿µ - .outer { - font-size: 40px; - text-align: center; - background-color: #9b7a1b; - padding: 120px; - } +以ç¹å»äºä»¶ä¸ºä¾ï¼æ¯å¦ä¸ä¸ªç¹å»äºä»¶åçå¨æä¸ª `<video>` å ç´ ä¸ï¼è¿ä¸ªå ç´ æä¸ä¸ªç¶å ç´ ï¼æ¯å¦æ¯ä¸ª `div` å ç´ ï¼ï¼æµè§å¨ä¼æ§è¡ä¸¤ä¸ªå¤çé¶æ®µ - æè·ï¼capturingï¼é¶æ®µååæ³¡ï¼bubblingï¼é¶æ®µãå¨ web å¼åä¸åæ³¡é¶æ®µä¼ç¨çæ¯è¾å¤ï¼èæè·å¤çç¨çæ¯è¾å°ã - .root { - font-size: 40px; - text-align: center; - background-color: #7a1b9b; - padding: 80px; - } -</style> -``` +卿è·é¶æ®µï¼æµè§å¨æ£æ¥å½åå ç´ çæå¤å±ç¶èç¹ï¼å¨ web ä¸ï¼æ¯å¦ï¼`<html>` å ç´ ï¼ï¼å¦æå®ä¸é¢ç»å®äºä¸ä¸ª click äºä»¶å¤çå¨ï¼é£ä¹å æ§è¡è¿ä¸ªå¤çå¨ãç¶åæ£æ¥ä¸ä¸ä¸ªå ç´ ï¼`<html>` çåå ç´ éæ¯ `<video>` ç¥å å ç´ çé£ä¸ªå ç´ ï¼ååæ ·çæ£æµã䏿¥æ¥ç´å°éå°å½åç¹å»çè¿ä¸ªå ç´ æ¬èº«ã -[try it](http://dotwe.org/vue/fa2957ce3e9eb47ad9ae1da22d845e95) +æ¥ä¸æ¥æ¯åæ³¡é¶æ®µï¼æ¹ååæè·é¶æ®µç¸åï¼æµè§å¨å æ£æµå½å被ç¹å»çå ç´ ä¸æ¯å¦æ³¨åäºç¹å»äºä»¶å¤çå¨ï¼å¦ææåæ§è¡å®ãæ¥ä¸æ¥æ£æµå®çç¶å ç´ ï¼ä¸æ¥æ¥åä¸ï¼ç´å°æå¤å± `<html>` å ç´ ã -è¿è¡ä»¥ä¸ä»£ç ï¼ç¨å®¢æ·ç«¯æå¼ï¼ç¹å»ä¸é´çå ç´ ï¼å¯ä»¥çå°äºä»¶åä¸ä¼ æï¼ä¾æ¬¡è§¦åã + -### 注æ +æä»¬ä¸è¬ä½¿ç¨é»è®¤çäºä»¶æ³¨åæºå¶ï¼å°äºä»¶å¤ç注åå¨åæ³¡é¶æ®µï¼æä»¥å¤çåæ³¡é¶æ®µçæ 嵿¯è¾å¤ã彿们æ³è¦åæ¢äºä»¶å泡ï¼åªéè¦è°ç¨äºä»¶å¯¹è±¡ç `stopPropagation` æ¹æ³ãæ åäºä»¶å¯¹è±¡å å« `stopPropagation` æ¹æ³ï¼å½æ§è¡äºä»¶å¤ç卿¶è°ç¨è¯¥æ¹æ³ï¼ä¼ç«å³åæ¢äºä»¶å泡ï¼è¿æ ·äºä»¶å泡å¤çé¾ä¸çåç»å¤çå¨å°±ä¸ä¼åæ§è¡ä¸å»ã -éè¦æ³¨æçæ¯: ** 为äºå ¼å®¹ä¹åççæ¬ï¼Weex é»è®¤ä¸ä¼å¼å¯äºä»¶å泡æºå¶ãéè¦å¨æ ¹å ç´ ç屿§ä¸ï¼æ·»å `bubble="true"` æ¥å¼å¯å泡æºå¶ãå¦åï¼å°ä¸ä¼åä¸ä¼ æäºä»¶ï¼ä¿æä¸ä¹åçæ¬çææç¸åã ** +Weex å¨ 0.13 çæ¬ SDK éå®ç°äºäºä»¶å泡æºå¶ã注æäºä»¶å泡é»è®¤æ¯ä¸å¼å¯çï¼ä½ éè¦å¨æ¨¡æ¿æ ¹å ç´ ä¸å ä¸å±æ§ `bubble=true` æè½å¼å¯å泡ã -### stopPropagation +## ä½¿ç¨ -å¨äºä»¶å¤ç彿°ä¸ï¼å¯ä»¥ä½¿ç¨ `e.stopPropagation()` æ¹æ³ï¼æ¥é»æ¢æ¬æ¬¡äºä»¶åä¸çä¼ éè¿ç¨ã注æï¼`e.stopPropagation()` ä¸ `bubble="true"` ä¸åï¼åè åªä¼å½±åå½åå ç´ ä»¥åç¶å ç´ çä¼ æï¼ä¸ä¼å½±ååå ç´ çä¼ æï¼åè æ¯ä¸ºäºçæ¬å ¼å®¹èå¢å çå¼å ³æºå¶ï¼ä¼å ¨å±å ³éæè å¼å¯å泡æºå¶ï¼ä¸¤è å¯ä»¥å ±ååå¨ä½¿ç¨ï¼å¦ä¸ï¼ +äºä»¶å泡é»è®¤ä¸å¼å¯ï¼éè¦æå¨æ·»å `bubble=true` 屿§å°æ ¹å ç´ ä¸ã ```html <template> - <div class="root" @click="rootClick" bubble="true"> - <text style="font-size: 40px;">{{rootText}}</text> - <div class="outer" @click="parentClick"> - <div> - <text style="font-size: 40px;">{{parentText}}</text> - </div> - <text class="inner" @click="click">{{innerText}}</text> - </div> + <!-- å¼å¯äºä»¶å泡æºå¶. --> + <div bubble="true"> + ... </div> </template> +``` -<script> - module.exports = { - data: { - innerText: 'click me', - parentText: '', - rootText: '' - }, - methods: { - click: function(e) { - this.innerText = 'inner bubble' - }, - parentClick: function(e) { - this.parentText = 'parent bubble' - e.stopPropagation() - }, - rootClick: function(e) { - this.rootText = 'root bubble' - // e.stopPropagation() - } - } - } -</script> - -<style scoped> - .inner { - font-size: 40px; - text-align: center; - background-color: #7a9b1b; - padding: 40px; - } +## 黿¢å泡 - .outer { - font-size: 40px; - text-align: center; - background-color: #9b7a1b; - padding: 120px; - } +å¨äºä»¶å¤ç彿°éï¼å¯ä»¥éè¿è°ç¨ `event.stopPropagation` æ¹æ³é»æ¢äºä»¶å泡ãè¿ä¸ªæ¹æ³å [DOM æ å](https://dom.spec.whatwg.org/#dom-event-stoppropagation)éçæ¹æ³ä¸è´ã注æ `event.stopPropagation` å `bubble=true` çå½±åèå´ä¸åï¼åè ä» é对å½ååæ³¡å°çå ç´ ä»¥åå ¶ç¥å å±çº§ææï¼è对åå ç´ æ æãèåè ç¸å½äºä¸ä¸ªå ¨å±å¼å ³ï¼å¼å¯ä»¥åå¯¹è¯¥æ ¹å ç´ å 鍿æåå ç´ é½å¼å¯äºäºä»¶å泡ææã两è å¯ä»¥åæ¶åå¨ã - .root { - font-size: 40px; - text-align: center; - background-color: #7a1b9b; - padding: 80px; +```javascript +{ + handleClick (event) { + // stop event propagation. + event.stopPropagation() } -</style> +} ``` -[try it](http://dotwe.org/vue/2cc80e19c9b2430fb780234628065a69) +## 注æ + +éè¦æ³¨æçæ¯: **为äºå ¼å®¹ä¹åççæ¬ï¼Weex é»è®¤ä¸ä¼å¼å¯äºä»¶å泡æºå¶ãéè¦å¨æ ¹å ç´ ç屿§ä¸ï¼æ·»å `bubble="true"` æ¥å¼å¯å泡æºå¶ãå¦åï¼å°ä¸ä¼åä¸ä¼ æäºä»¶ï¼ä¿æä¸ä¹åçæ¬çææç¸åã** + +## å®ä¾ + +[å¼å¯äºä»¶å泡](http://dotwe.org/vue/fa2957ce3e9eb47ad9ae1da22d845e95)ï¼ä½¿ç¨ Weex playground App æ«æé¡µé¢éçäºç»´ç ï¼ç¶å卿å¼é¡µé¢ä¸ç¹å»å å« 'click me' æåçæ¹æ¡ï¼ä¼çå°äºä»¶å泡ææï¼å³å¤å±çç»ä»¶ä¾æ¬¡æ¾ç¤ºäºä»¶æååæ³¡å°å½åç»ä»¶çæç¤ºã -è¿è¡ä»¥ä¸ä»£ç ï¼ç¨å®¢æ·ç«¯æå¼ï¼ç¹å»ä¸é´çå ç´ ï¼å¯ä»¥çå°äºä»¶åä¸ä¼ æå°ç¶å ç´ è¢«ç»æ¢ï¼ä¸åç»§ç»å¾æ ¹å ç´ ä¼ æã +[黿¢å泡](http://dotwe.org/vue/2cc80e19c9b2430fb780234628065a69)ï¼ä½¿ç¨ Weex playground App æ«æé¡µé¢éçäºç»´ç ï¼ç¶å卿å¼é¡µé¢ä¸ç¹å»å å« 'click me' æåçæ¹æ¡ï¼å¯ä»¥çå°äºä»¶å泡被ç¶ç»ä»¶ä¸æäºï¼ä¸ååæ³¡å°æå¤å±ç»ä»¶ã http://git-wip-us.apache.org/repos/asf/incubator-weex-site/blob/f1ddc1ec/source/wiki/event-bubble.md ---------------------------------------------------------------------- diff --git a/source/wiki/event-bubble.md b/source/wiki/event-bubble.md index 261266a..f470a7e 100644 --- a/source/wiki/event-bubble.md +++ b/source/wiki/event-bubble.md @@ -8,142 +8,60 @@ version: 2.1 <!-- toc --> -# Event Bubble <span class="api-version">v0.13+</span> +<span class="api-version">v0.13+</span> -Weex 2.0 implements the W3C standard event bubbling mechanism. +> **Note:** This feature works only on weex's native platforms (i.e., on Android and iOS), but not on the web with latest [web renderer](https://github.com/weexteam/weex-vue-render) yet. -### Usage +If you are a web developer, then you are probably familiar with the event bubbling mechanism, and you may expect Weex to work the same way. However, Weex didn't implement this mechiansim util version 0.13. -```html -<template> - <div class="root" @click="rootClick" bubble="true"> - <text style="font-size: 40px;">{{rootText}}</text> - <div class="outer" @click="parentClick"> - <div> - <text style="font-size: 40px;">{{parentText}}</text> - </div> - <text class="inner" @click="click">{{innerText}}</text> - </div> - </div> -</template> +To those developers who are not familiar with the event bubbling, Here are some explanations about it, and the concept is rather easy. -<script> - module.exports = { - data: { - innerText: 'click me', - parentText: '', - rootText: '' - }, - methods: { - click: function(e) { - this.innerText = 'inner bubble' - }, - parentClick: function(e) { - this.parentText = 'parent bubble' - }, - rootClick: function(e) { - this.rootText = 'root bubble' - } - } - } -</script> - -<style scoped> - .inner { - font-size: 40px; - text-align: center; - background-color: #7a9b1b; - padding: 40px; - } +## Concept - .outer { - font-size: 40px; - text-align: center; - background-color: #9b7a1b; - padding: 120px; - } +Take clicking as a example. When a click event fired on a `<video>` element that has a parent element (e.g. a parent `div` component), web browsers run two different phases - the capturing phase and the bubbling phase. We use bubbling phase a lot in web development, and use capturing less. - .root { - font-size: 40px; - text-align: center; - background-color: #7a1b9b; - padding: 80px; - } -</style> -``` +In the capturing phase, The browser checks to see if the element's outer-most ancestor (on web, may be a `<html>`) has an corresponding event handler registered on it, and runs it if so. Then it moves on to the next element inside `<html>` and does the same thing, then the next one, and so on until it reaches the element that was actually clicked on. -[try it](http://dotwe.org/vue/fa2957ce3e9eb47ad9ae1da22d845e95) +In the bubbling phase, the exact opposite occurs: The browser checks to see if the element that was actually clicked on has an onclick event handler registered on it in the bubbling phase, and runs it if so. Then it moves on to the next immediate ancestor element and does the same thing, then the next one, and so on until it reaches the `<html>` element. -Run the above code, open with the client, click on the middle of the elements, you can see the event spread up, followed by the trigger. + -### Notice +We usually register our event listeners on the bubbling phase, so that we use bubbling a lot. But if you want to turn this off, you can use `stopPropagation` method of the event object. The standard event object has a function available on it called `stopPropagation`, which when invoked on a handler's event object makes it so that handler is run, but the event doesn't bubble any further up the chain, so no more handlers will be run. -One thing should be noticed: **For compatibility with previous versions, Weex does not turn on event bubbling by default. You need to add `bubble = "true"` on the root element's properties to turn on the bubbling mechanism. Otherwise, the event will not be propagated upwards, keeping the same effect as the previous version.** +Weex implemented this event bubbling phase in the 0.13 SDK, and for the record, the event bubbling phase is not enabled by default - You should use a attribute `bubble=true` on the root element to turn this feature on. -### stopPropagation +## How To Use -In the event handler function, you can use the `e.stopPropagation()` method to prevent the event from escalating. Note that `e.stopPropagation()` differs from `bubble = "true"`, which affects only the current elements and the propagation of parent elements, without affecting the propagation of child elements; the latter is a switching mechanism that is added for compatibility, Will be a global shutdown or open the bubble mechanism, the two can co-exist, as follows: +The event bubbling is not enabled by default, you should use `bubble="true"` in the root element to turn it on. ```html <template> - <div class="root" @click="rootClick" bubble="true"> - <text style="font-size: 40px;">{{rootText}}</text> - <div class="outer" @click="parentClick"> - <div> - <text style="font-size: 40px;">{{parentText}}</text> - </div> - <text class="inner" @click="click">{{innerText}}</text> - </div> + <!-- Use it in the root element to turn it on. --> + <div bubble="true"> + ... </div> </template> +``` -<script> - module.exports = { - data: { - innerText: 'click me', - parentText: '', - rootText: '' - }, - methods: { - click: function(e) { - this.innerText = 'inner bubble' - }, - parentClick: function(e) { - this.parentText = 'parent bubble' - e.stopPropagation() - }, - rootClick: function(e) { - this.rootText = 'root bubble' - // e.stopPropagation() - } - } - } -</script> - -<style scoped> - .inner { - font-size: 40px; - text-align: center; - background-color: #7a9b1b; - padding: 40px; - } +## Stop Bubbling - .outer { - font-size: 40px; - text-align: center; - background-color: #9b7a1b; - padding: 120px; - } +In the event handler function, you can use the `event.stopPropagation()` method to prevent the event from escalating, which is exactly like the event's method with the same name in [DOM standard](https://dom.spec.whatwg.org/#dom-event-stoppropagation). Note that `event.stopPropagation()` differs from `bubble = "true"`, which affects only the current elements and the propagation of parent elements, without affecting the propagation of child elements; the latter is a switching mechanism that is added for compatibility, Will be a global shutdown or open the bubble mechanism, the two can co-exist. - .root { - font-size: 40px; - text-align: center; - background-color: #7a1b9b; - padding: 80px; +```javascript +{ + handleClick (event) { + // stop event propagation. + event.stopPropagation() } -</style> +} ``` -[try it](http://dotwe.org/vue/2cc80e19c9b2430fb780234628065a69) +## Notice + +One thing should be noticed: **For compatibility reason, Weex does not turn on event bubbling by default. You need to add `bubble = "true"` on the root element's properties to turn on the bubbling mechanism. Otherwise, Weex will keep working in the old way, without the event bubbling effect.** + +## Examples + +[enable event bubbling](http://dotwe.org/vue/fa2957ce3e9eb47ad9ae1da22d845e95): Use Weex playground APP to scan the qr-code, and then click on the middle of the rectangle with a line of text 'click me' on, you can see the event bubbling up, being handled with every listener binding on each div element, printing messages which indicate that they are indeed been reached by the event. -Run the above code, open with the client, click on the middle of the element, you can see the event up to the parent element is terminated, no longer continue to spread to the root element. +[stop event propagation](http://dotwe.org/vue/2cc80e19c9b2430fb780234628065a69): Use Weex playground APP to scan the qr-code, and then click on the middle of the rectangle with a line of text 'click me' on, you can see the event bubbling is disturbed, no longer continue to spread to the root element.
