Skip to content

Commit 6bc60e9

Browse files
committed
add v-events directive
1 parent bf9edef commit 6bc60e9

File tree

6 files changed

+124
-11
lines changed

6 files changed

+124
-11
lines changed

component.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"src/directives/cloak.js",
2929
"src/directives/component.js",
3030
"src/directives/el.js",
31+
"src/directives/events.js",
3132
"src/directives/html.js",
3233
"src/directives/if.js",
3334
"src/directives/index.js",

src/directives/events.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var _ = require('../util')
2+
3+
module.exports = {
4+
5+
bind: function () {
6+
var child = this.el.__vue__
7+
if (!child || this.vm !== child.$parent) {
8+
_.warn(
9+
'`v-events` should only be used on a child component ' +
10+
'from the parent template.'
11+
)
12+
return
13+
}
14+
var method = this.vm[this.expression]
15+
if (!method) {
16+
_.warn(
17+
'`v-events` cannot find method "' + this.expression +
18+
'" on the parent instance.'
19+
)
20+
}
21+
child.$on(this.arg, method)
22+
}
23+
24+
// when child is destroyed, all events are turned off,
25+
// so no need for unbind here.
26+
27+
}

src/directives/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ exports.model = require('./model')
1919
exports.component = require('./component')
2020
exports.repeat = require('./repeat')
2121
exports['if'] = require('./if')
22-
exports['with'] = require('./with')
22+
23+
// child vm communication directives
24+
exports['with'] = require('./with')
25+
exports.events = require('./events')

src/directives/ref.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@ module.exports = {
66

77
bind: function () {
88
var child = this.el.__vue__
9-
if (!child) {
9+
if (!child || this.vm !== child.$parent) {
1010
_.warn(
11-
'v-ref should only be used on instance root nodes.'
12-
)
13-
return
14-
}
15-
if (this.vm !== child.$parent) {
16-
_.warn(
17-
'v-ref should be used from the parent template,' +
18-
' not the component\'s.'
11+
'v-ref should only be used on a child component ' +
12+
'from the parent template.'
1913
)
2014
return
2115
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var _ = require('../../../../src/util')
2+
var Vue = require('../../../../src/vue')
3+
4+
if (_.inBrowser) {
5+
describe('v-events', function () {
6+
7+
var el
8+
beforeEach(function () {
9+
spyOn(_, 'warn')
10+
el = document.createElement('div')
11+
})
12+
13+
it('should register events', function () {
14+
var spy = jasmine.createSpy('v-events')
15+
new Vue({
16+
el: el,
17+
template: '<div v-component="test" v-events="test:test"></div>',
18+
methods: {
19+
test: spy
20+
},
21+
components: {
22+
test: {
23+
compiled: function () {
24+
this.$emit('test')
25+
}
26+
}
27+
}
28+
})
29+
expect(spy).toHaveBeenCalled()
30+
})
31+
32+
it('should warn when used on non-root node', function () {
33+
new Vue({
34+
el: el,
35+
template: '<div v-events="test:test"></div>'
36+
})
37+
expect(_.warn).toHaveBeenCalled()
38+
})
39+
40+
it('should warn when used on child component root', function () {
41+
var spy = jasmine.createSpy('v-events')
42+
new Vue({
43+
el: el,
44+
template: '<div v-component="test"></div>',
45+
methods: {
46+
test: spy
47+
},
48+
components: {
49+
test: {
50+
replace: true,
51+
template: '<div v-events="test:test"></div>',
52+
compiled: function () {
53+
this.$emit('test')
54+
}
55+
}
56+
}
57+
})
58+
expect(_.warn).toHaveBeenCalled()
59+
expect(spy).not.toHaveBeenCalled()
60+
})
61+
62+
it('should warn when method not found on parent', function () {
63+
new Vue({
64+
el: el,
65+
template: '<div v-component="test" v-events="test:test"></div>',
66+
components: {
67+
test: {}
68+
}
69+
})
70+
expect(_.warn).toHaveBeenCalled()
71+
})
72+
73+
})
74+
}

test/unit/specs/directives/ref_spec.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,27 @@ if (_.inBrowser) {
6363
expect(vm.$.c1.$.c2.length).toBe(2)
6464
})
6565

66-
it('warn on non-root', function () {
66+
it('should warn on non-root', function () {
6767
var vm = new Vue({
6868
el: el,
6969
template: '<div v-ref="test"></div>'
7070
})
7171
expect(_.warn).toHaveBeenCalled()
7272
})
7373

74+
it('should warn when used in child template', function () {
75+
var vm = new Vue({
76+
el: el,
77+
template: '<div v-component="test"></div>',
78+
components: {
79+
test: {
80+
template: '<div v-ref="test"></div>',
81+
replace: true
82+
}
83+
}
84+
})
85+
expect(_.warn).toHaveBeenCalled()
86+
})
87+
7488
})
7589
}

0 commit comments

Comments
 (0)