@@ -74,6 +74,68 @@ function checkBubble (el) {
74
74
) [ 0 ]
75
75
}
76
76
77
+ /**
78
+ * bind events and nativeEvents:
79
+ * - with appear events: weex-appear, data-evt-xxx
80
+ * - with all weex events: stop propagation by default.
81
+ * - with click and scroll events: weex$tap and weex$scroll.
82
+ */
83
+ function bindEvents ( evts , el , attrs , weexEvents ) {
84
+ const evtKeys = Object . keys ( evts )
85
+ el . _markedAppear = false
86
+ for ( let i = 0 , l = evtKeys . length ; i < l ; i ++ ) {
87
+ const key = evtKeys [ i ]
88
+ const transKey = transEvtsMap [ key ]
89
+ const evtName = transKey || key
90
+ if ( transKey && ! el . _markedAppear ) {
91
+ el . _markedAppear = true
92
+ attrs . push ( {
93
+ name : `weex-appear` ,
94
+ value : '""'
95
+ } )
96
+ }
97
+ attrs . push ( {
98
+ name : `data-evt-${ evtName } ` ,
99
+ value : '""'
100
+ } )
101
+ }
102
+
103
+ const hasBubbleParent = checkBubble ( el )
104
+
105
+ /**
106
+ * stop propagation by default unless attr 'bubble' is set to true.
107
+ * only for weex events, user defined events should not be stopped
108
+ * by default.
109
+ */
110
+ if ( ! hasBubbleParent ) {
111
+ for ( const k in evts ) {
112
+ if ( weexEvents . indexOf ( k ) > - 1 ) {
113
+ const evt = evts [ k ]
114
+ const modifiers = evt . modifiers || ( evt . modifiers = { } )
115
+ modifiers . stop = true
116
+ }
117
+ }
118
+ }
119
+
120
+ /**
121
+ * map event handlers.
122
+ * - click -> weex$tap
123
+ * - scroll -> weex$scroll
124
+ */
125
+ if ( evts . click ) {
126
+ evts [ 'weex$tap' ] = extend ( { } , evts . click )
127
+ if ( ! hasBubbleParent ) {
128
+ evts . click = {
129
+ value : '$stopOuterA'
130
+ }
131
+ }
132
+ }
133
+ if ( evts . scroll ) {
134
+ evts [ 'weex$scroll' ] = evts . scroll
135
+ delete evts . scroll
136
+ }
137
+ }
138
+
77
139
module . exports = function eventsHook (
78
140
el ,
79
141
attrsMap ,
@@ -89,64 +151,23 @@ module.exports = function eventsHook (
89
151
90
152
const { weexEvents } = this . config
91
153
let evts = el . events
92
- // bind events to events and remove nativeEvents.
93
- delete el . nativeEvents
154
+ const nativeEvts = el . nativeEvents
94
155
95
- if ( evts ) {
96
- const evtKeys = Object . keys ( evts )
97
- let marked = false
98
- for ( let i = 0 , l = evtKeys . length ; i < l ; i ++ ) {
99
- const key = evtKeys [ i ]
100
- const transKey = transEvtsMap [ key ]
101
- const evtName = transKey || key
102
- if ( transKey && ! marked ) {
103
- marked = true
104
- attrs . push ( {
105
- name : `weex-appear` ,
106
- value : '""'
107
- } )
108
- }
109
- attrs . push ( {
110
- name : `data-evt-${ evtName } ` ,
111
- value : '""'
112
- } )
156
+ try {
157
+ if ( evts ) {
158
+ bindEvents ( evts , el , attrs , weexEvents )
113
159
}
114
-
115
- const hasBubbleParent = checkBubble ( el )
116
-
117
- /**
118
- * stop propagation by default unless attr 'bubble' is set to true.
119
- * only for weex events, user defined events should not be stopped
120
- * by default.
121
- */
122
- if ( ! hasBubbleParent ) {
123
- for ( const k in evts ) {
124
- if ( weexEvents . indexOf ( k ) > - 1 ) {
125
- const evt = evts [ k ]
126
- const modifiers = evt . modifiers || ( evt . modifiers = { } )
127
- modifiers . stop = true
128
- }
129
- }
130
- }
131
-
132
- /**
133
- * map event handlers.
134
- * - click -> weex$tap
135
- * - scroll -> weex$scroll
136
- */
137
- if ( evts . click ) {
138
- evts [ 'weex$tap' ] = extend ( { } , evts . click )
139
- if ( ! hasBubbleParent ) {
140
- evts . click = {
141
- value : '$stopOuterA'
142
- }
143
- }
160
+ if ( nativeEvts ) {
161
+ bindEvents ( nativeEvts , el , attrs , weexEvents )
144
162
}
145
- if ( evts . scroll ) {
146
- evts [ 'weex$scroll' ] = evts . scroll
147
- delete evts . scroll
163
+ else {
164
+ delete el . nativeEvents
148
165
}
166
+ } catch ( err ) {
167
+ console . error ( 'err=======>' , err )
149
168
}
169
+
170
+ delete el . _markedAppear
150
171
151
172
/**
152
173
* binding a weex$tap to <a> element to stop propagation if there
0 commit comments