diff --git a/src/directives/events.js b/src/directives/events.js index 85a52e65cbb..df185c98194 100644 --- a/src/directives/events.js +++ b/src/directives/events.js @@ -1,6 +1,8 @@ var _ = require('../util') -module.exports = { +module.exports = { + + acceptStatement: true, bind: function () { var child = this.el.__vue__ @@ -11,14 +13,21 @@ module.exports = { ) return } - var method = this.vm[this.expression] - if (!method) { + }, + + update: function (handler, oldHandler) { + if (typeof handler !== 'function') { _.warn( - '`v-events` cannot find method "' + this.expression + - '" on the parent instance.' + 'Directive "v-events:' + this.expression + '" ' + + 'expects a function value.' ) + return + } + var child = this.el.__vue__ + if (oldHandler) { + child.$off(this.arg, oldHandler) } - child.$on(this.arg, method) + child.$on(this.arg, handler) } // when child is destroyed, all events are turned off, diff --git a/test/unit/specs/directives/events_spec.js b/test/unit/specs/directives/events_spec.js index 0d722780aba..ea59a887083 100644 --- a/test/unit/specs/directives/events_spec.js +++ b/test/unit/specs/directives/events_spec.js @@ -59,9 +59,10 @@ if (_.inBrowser) { expect(spy).not.toHaveBeenCalled() }) - it('should warn when method not found on parent', function () { - new Vue({ + it('should warn on non-function values', function () { + var vm = new Vue({ el: el, + data: { test: 123 }, template: '
', components: { test: {} @@ -70,5 +71,24 @@ if (_.inBrowser) { expect(_.warn).toHaveBeenCalled() }) + it('should accept inline statement', function (done) { + var vm = new Vue({ + el: el, + data: {a:1}, + template: '
', + components: { + test: { + compiled: function () { + this.$emit('test') + } + } + } + }) + _.nextTick(function () { + expect(vm.a).toBe(2) + done() + }) + }) + }) } \ No newline at end of file