Skip to content

Commit 3d95654

Browse files
committed
support render-function & support .native modifier for user defined cmps.
1 parent 785877e commit 3d95654

File tree

11 files changed

+171
-25
lines changed

11 files changed

+171
-25
lines changed

src/config.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@ const config = {
33
click: 'weex$tap',
44
scroll: 'weex$scroll'
55
},
6+
// these components should auto bind events with .native.
7+
weexRegisteredComponents: [
8+
'input',
9+
'switch',
10+
'list',
11+
'scroller',
12+
'waterfall',
13+
'header',
14+
'loading',
15+
'refresh',
16+
'loading-indicator',
17+
'slider',
18+
'cycleslider',
19+
'slider-neighbor',
20+
'indicator',
21+
'textarea',
22+
'video',
23+
'web'
24+
],
25+
// these components should not bind events with .native.
26+
weexBuiltInComponents: [
27+
'div',
28+
'container',
29+
'text',
30+
'image',
31+
'img',
32+
'cell',
33+
'a'
34+
],
635
// add .stop to stop propagation for weex native events only.
736
// user defined events may not have the stopPropagation method.
837
weexEvents: [

src/hooks/events.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,14 @@ function checkBubble (el) {
8080
* - with all weex events: stop propagation by default.
8181
* - with click and scroll events: weex$tap and weex$scroll.
8282
*/
83-
function bindEvents(evts, el, attrs, weexEvents) {
83+
function bindEvents(evts, el, attrs, weexEvents, appearAttached) {
8484
const evtKeys = Object.keys(evts)
85-
el._markedAppear = false
8685
for (let i = 0, l = evtKeys.length; i < l; i++) {
8786
const key = evtKeys[i]
8887
const transKey = transEvtsMap[key]
8988
const evtName = transKey || key
90-
if (transKey && !el._markedAppear) {
91-
el._markedAppear = true
89+
if (transKey && !appearAttached.value) {
90+
appearAttached.value = true
9291
attrs.push({
9392
name: `weex-appear`,
9493
value: '""'
@@ -151,23 +150,44 @@ module.exports = function eventsHook (
151150

152151
const { weexEvents } = this.config
153152
let evts = el.events
154-
const nativeEvts = el.nativeEvents
153+
let nativeEvts = el.nativeEvents
155154

156-
try {
155+
if (nativeEvts && el._builtIn) {
156+
/**
157+
* for div, image, text, cell, a, ...
158+
* user should bind all events without .native.
159+
*/
160+
nativeEvts = null
161+
delete el.nativeEvents
162+
}
163+
if (el._weexRegistered) {
164+
/**
165+
* for slider, list, ...
166+
* user should bind events without .native.
167+
* in our events handling, all events should transfer to
168+
* .native binding.
169+
*/
170+
delete el.nativeEvents
171+
nativeEvts = null
157172
if (evts) {
158-
bindEvents(evts, el, attrs, weexEvents)
159-
}
160-
if (nativeEvts) {
161-
bindEvents(nativeEvts, el, attrs, weexEvents)
173+
nativeEvts = el.nativeEvents = evts
162174
}
163-
else {
164-
delete el.nativeEvents
165-
}
166-
} catch (err) {
167-
console.error('err=======>', err)
175+
evts = null
176+
delete el.events
177+
}
178+
179+
const appearAttached = {
180+
value: false
181+
}
182+
if (evts) {
183+
bindEvents(evts, el, attrs, weexEvents, appearAttached)
184+
}
185+
if (nativeEvts) {
186+
bindEvents(nativeEvts, el, attrs, weexEvents, appearAttached)
187+
}
188+
else {
189+
delete el.nativeEvents
168190
}
169-
170-
delete el._markedAppear
171191

172192
/**
173193
* binding a weex$tap to <a> element to stop propagation if there

src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ class Precompiler {
3131
const args = [el, attrsMap, attrsList, attrs, staticClass]
3232

3333
const tag = identifyTag(el)
34+
35+
const { weexBuiltInComponents, weexRegisteredComponents } = this.config
36+
if (weexBuiltInComponents.indexOf(el._origTag || el.tag) > -1) {
37+
el._weexBuiltIn = true
38+
}
39+
else if (weexRegisteredComponents.indexOf(el.tag) > -1) {
40+
el._weexRegistered = true
41+
}
42+
else {
43+
el._userRegistered = true
44+
}
45+
3446
// use component processors to process components' special attrs.
3547
const processor = components[tag]
3648
if (processor) {

test/input/hooks/events.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<template>
22
<div>
3+
<some-cmp @someEvent="someHandler" @appear.native="appear"></some-cmp>
4+
<slider @change="change" @appear="appear"></slider>
35
<div @click="click" @appear="appear"></div>
46
</div>
57
</template>

test/output/components/a.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = [
22
{
33
type: 1,
44
tag: 'a',
5+
_weexBuiltIn: true,
56
_hasBubbleParent: false,
67
events: {
78
'weex$tap': {
@@ -23,6 +24,7 @@ module.exports = [
2324
}, {
2425
type: 1,
2526
tag: 'div',
27+
_weexBuiltIn: true,
2628
plain: false,
2729
attrs: [
2830
{

test/output/components/cell.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = [
33
type: 1,
44
tag: 'section',
55
_origTag: 'cell',
6+
_weexBuiltIn: true,
67
plain: false,
78
static: false,
89
attrs: [

test/output/components/custom.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = [
33
type: 1,
44
tag: 'custom-component',
55
_hasBubbleParent: false,
6+
_userRegistered: true,
67
nativeEvents: {
78
'click': {
89
value: '$stopOuterA'

test/output/components/image.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = [
33
type: 1,
44
tag: 'figure',
55
_origTag: 'image',
6+
_weexBuiltIn: true,
67
plain: false,
78
hasBindings: true,
89
attrs: [
@@ -36,6 +37,8 @@ module.exports = [
3637
}, {
3738
type: 1,
3839
tag: 'div',
40+
_origTag: 'div',
41+
_weexBuiltIn: true,
3942
plain: false,
4043
attrs: [
4144
{
@@ -44,7 +47,6 @@ module.exports = [
4447
}
4548
],
4649
staticClass: '" weex-ct weex-div"',
47-
static: false,
48-
_origTag: 'div'
50+
static: false
4951
}
5052
]

test/output/components/text.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = [
55
type: 1,
66
tag: 'p',
77
_origTag: 'text',
8+
_weexBuiltIn: true,
89
plain: false,
910
attrs: [
1011
{
@@ -25,6 +26,8 @@ module.exports = [
2526
}, {
2627
type: 1,
2728
tag: 'div',
29+
_origTag: 'div',
30+
_weexBuiltIn: true,
2831
plain: false,
2932
attrs: [
3033
{
@@ -33,7 +36,6 @@ module.exports = [
3336
}
3437
],
3538
staticClass: '" weex-ct weex-div"',
36-
static: false,
37-
_origTag: 'div'
39+
static: false
3840
}
3941
]

test/output/hooks/a-bubble.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module.exports = [
44
ref: '"inner"',
55
refInFor: false,
66
tag: 'div',
7+
_origTag: 'div',
8+
_weexBuiltIn: true,
79
plain: false,
810
hasBindings: true,
911
_hasBubbleParent: false,
@@ -27,11 +29,11 @@ module.exports = [
2729
value: '""'
2830
}
2931
],
30-
staticClass: '" weex-ct weex-div"',
31-
_origTag: 'div'
32+
staticClass: '" weex-ct weex-div"'
3233
}, {
3334
type: 1,
3435
tag: 'a',
36+
_weexBuiltIn: true,
3537
_hasBubbleParent: false,
3638
events: {
3739
'weex$tap': {
@@ -52,6 +54,8 @@ module.exports = [
5254
}, {
5355
type: 1,
5456
tag: 'div',
57+
_origTag: 'div',
58+
_weexBuiltIn: true,
5559
ref: '"outer"',
5660
refInFor: false,
5761
_hasBubbleParent: false,
@@ -78,7 +82,6 @@ module.exports = [
7882
],
7983
hasBindings: true,
8084
staticClass: '" weex-ct weex-div"',
81-
static: false,
82-
_origTag: 'div'
85+
static: false
8386
}
8487
]

0 commit comments

Comments
 (0)