Skip to content

Commit af0f69a

Browse files
committed
Merge pull request #808 from dgerber/dev
let v-events accept statements
2 parents 4b20ad6 + 9587d10 commit af0f69a

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/directives/events.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var _ = require('../util')
22

3-
module.exports = {
3+
module.exports = {
4+
5+
acceptStatement: true,
46

57
bind: function () {
68
var child = this.el.__vue__
@@ -11,14 +13,21 @@ module.exports = {
1113
)
1214
return
1315
}
14-
var method = this.vm[this.expression]
15-
if (!method) {
16+
},
17+
18+
update: function (handler, oldHandler) {
19+
if (typeof handler !== 'function') {
1620
_.warn(
17-
'`v-events` cannot find method "' + this.expression +
18-
'" on the parent instance.'
21+
'Directive "v-events:' + this.expression + '" ' +
22+
'expects a function value.'
1923
)
24+
return
25+
}
26+
var child = this.el.__vue__
27+
if (oldHandler) {
28+
child.$off(this.arg, oldHandler)
2029
}
21-
child.$on(this.arg, method)
30+
child.$on(this.arg, handler)
2231
}
2332

2433
// when child is destroyed, all events are turned off,

test/unit/specs/directives/events_spec.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ if (_.inBrowser) {
5959
expect(spy).not.toHaveBeenCalled()
6060
})
6161

62-
it('should warn when method not found on parent', function () {
63-
new Vue({
62+
it('should warn on non-function values', function () {
63+
var vm = new Vue({
6464
el: el,
65+
data: { test: 123 },
6566
template: '<div v-component="test" v-events="test:test"></div>',
6667
components: {
6768
test: {}
@@ -70,5 +71,24 @@ if (_.inBrowser) {
7071
expect(_.warn).toHaveBeenCalled()
7172
})
7273

74+
it('should accept inline statement', function (done) {
75+
var vm = new Vue({
76+
el: el,
77+
data: {a:1},
78+
template: '<div v-component="test" v-events="test:a++"></div>',
79+
components: {
80+
test: {
81+
compiled: function () {
82+
this.$emit('test')
83+
}
84+
}
85+
}
86+
})
87+
_.nextTick(function () {
88+
expect(vm.a).toBe(2)
89+
done()
90+
})
91+
})
92+
7393
})
7494
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy