File tree Expand file tree Collapse file tree 7 files changed +32
-7
lines changed
test/unit/features/directives Expand file tree Collapse file tree 7 files changed +32
-7
lines changed Original file line number Diff line number Diff line change @@ -102,6 +102,7 @@ declare type ASTElement = {
102
102
styleBinding ?: string ,
103
103
hooks ?: ASTElementHooks ,
104
104
events ?: ASTElementHandlers ,
105
+ nativeEvents ?: ASTElementHandlers ,
105
106
106
107
transition ?: string | true ,
107
108
transitionOnAppear ?: boolean ,
Original file line number Diff line number Diff line change @@ -44,7 +44,8 @@ declare interface VNodeData {
44
44
domProps ?: { [ key : string ] : any } ;
45
45
staticAttrs ?: { [ key : string ] : string } ;
46
46
hook ?: { [ key : string ] : Function } ;
47
- on ?: { [ key : string ] : Function | Array < Function > } ;
47
+ on ?: ?{ [ key : string ] : Function | Array < Function > } ;
48
+ nativeOn ?: { [ key : string ] : Function | Array < Function > } ;
48
49
transition ?: Object ;
49
50
inlineTemplate ?: {
50
51
render : Function ,
Original file line number Diff line number Diff line change @@ -21,8 +21,8 @@ const modifierCode = {
21
21
self : 'if($event.target !== $event.currentTarget)return;'
22
22
}
23
23
24
- export function genHandlers ( events : ASTElementHandlers ) : string {
25
- let res = 'on:{'
24
+ export function genHandlers ( events : ASTElementHandlers , native ?: boolean ) : string {
25
+ let res = native ? 'nativeOn:{' : 'on:{'
26
26
for ( const name in events ) {
27
27
res += `"${ name } ":${ genHandler ( events [ name ] ) } ,`
28
28
}
Original file line number Diff line number Diff line change @@ -166,6 +166,9 @@ function genData (el: ASTElement): string | void {
166
166
if ( el . events ) {
167
167
data += `${ genHandlers ( el . events ) } ,`
168
168
}
169
+ if ( el . nativeEvents ) {
170
+ data += `${ genHandlers ( el . nativeEvents , true ) } `
171
+ }
169
172
// inline-template
170
173
if ( el . inlineTemplate ) {
171
174
const ast = el . children [ 0 ]
Original file line number Diff line number Diff line change @@ -52,12 +52,18 @@ export function addHandler (
52
52
value : string ,
53
53
modifiers : ?{ [ key : string ] : true }
54
54
) {
55
- const events = el . events || ( el . events = { } )
56
55
// check capture modifier
57
56
if ( modifiers && modifiers . capture ) {
58
57
delete modifiers . capture
59
58
name = '!' + name // mark the event as captured
60
59
}
60
+ let events
61
+ if ( modifiers && modifiers . native ) {
62
+ delete modifiers . native
63
+ events = el . nativeEvents || ( el . nativeEvents = { } )
64
+ } else {
65
+ events = el . events || ( el . events = { } )
66
+ }
61
67
const newHandler = { value, modifiers }
62
68
const handlers = events [ name ]
63
69
/* istanbul ignore if */
Original file line number Diff line number Diff line change @@ -89,9 +89,8 @@ export function createComponent (
89
89
// extract listeners, since these needs to be treated as
90
90
// child component listeners instead of DOM listeners
91
91
const listeners = data . on
92
- if ( listeners ) {
93
- delete data . on
94
- }
92
+ // replace with listeners with .native modifier
93
+ data . on = data . nativeOn
95
94
96
95
// return a placeholder vnode
97
96
const name = Ctor . options . name || tag
Original file line number Diff line number Diff line change @@ -152,6 +152,21 @@ describe('Directive v-on', () => {
152
152
expect ( spy ) . toHaveBeenCalledWith ( 'foo' , 'bar' )
153
153
} )
154
154
155
+ it ( 'should be able to bind native events for a child component' , ( ) => {
156
+ Vue . component ( 'bar' , {
157
+ template : '<span>Hello</span>'
158
+ } )
159
+ vm = new Vue ( {
160
+ el,
161
+ template : '<bar @click.native="foo"></bar>' ,
162
+ methods : { foo : spy }
163
+ } )
164
+ vm . $children [ 0 ] . $emit ( 'click' )
165
+ expect ( spy ) . not . toHaveBeenCalled ( )
166
+ triggerEvent ( vm . $children [ 0 ] . $el , 'click' )
167
+ expect ( spy ) . toHaveBeenCalled ( )
168
+ } )
169
+
155
170
it ( 'remove listener' , done => {
156
171
const spy2 = jasmine . createSpy ( 'remove listener' )
157
172
vm = new Vue ( {
You can’t perform that action at this time.
0 commit comments