From 3b0537362e49abf9fc90ccc73ccfc07928a77fa4 Mon Sep 17 00:00:00 2001 From: Bojan Rajh Date: Wed, 1 Mar 2017 23:49:44 +0100 Subject: [PATCH] support multi event --- flow/component.js | 2 +- src/core/instance/events.js | 9 ++++++++- test/unit/features/instance/methods-events.spec.js | 10 ++++++++++ types/vue.d.ts | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/flow/component.js b/flow/component.js index 3c28f756a63..6b0490865a3 100644 --- a/flow/component.js +++ b/flow/component.js @@ -40,7 +40,7 @@ declare interface Component { $watch: (expOrFn: string | Function, cb: Function, options?: Object) => Function; $on: (event: string | Array, fn: Function) => Component; $once: (event: string, fn: Function) => Component; - $off: (event?: string, fn?: Function) => Component; + $off: (event?: string | Array, fn?: Function) => Component; $emit: (event: string, ...args: Array) => Component; $nextTick: (fn: Function) => void; $createElement: (tag?: string | Component, data?: Object, children?: VNodeChildren) => VNode; diff --git a/src/core/instance/events.js b/src/core/instance/events.js index af3fe01a381..bc278c6904c 100644 --- a/src/core/instance/events.js +++ b/src/core/instance/events.js @@ -66,13 +66,20 @@ export function eventsMixin (Vue: Class) { return vm } - Vue.prototype.$off = function (event?: string, fn?: Function): Component { + Vue.prototype.$off = function (event?: string | Array, fn?: Function): Component { const vm: Component = this // all if (!arguments.length) { vm._events = Object.create(null) return vm } + // array of events + if (Array.isArray(event)) { + for (let i = 0, l = event.length; i < l; i++) { + this.$off(event[i], fn) + } + return vm + } // specific event const cbs = vm._events[event] if (!cbs) { diff --git a/test/unit/features/instance/methods-events.spec.js b/test/unit/features/instance/methods-events.spec.js index a71022a6edb..be150076992 100644 --- a/test/unit/features/instance/methods-events.spec.js +++ b/test/unit/features/instance/methods-events.spec.js @@ -31,6 +31,16 @@ describe('Instance methods events', () => { expect(spy).toHaveBeenCalledWith(5, 6, 7, 8) }) + it('$off multi event', () => { + vm.$on(['test1', 'test2', 'test3'], spy) + vm.$off(['test1', 'test2'], spy) + vm.$emit('test1') + vm.$emit('test2') + expect(spy).not.toHaveBeenCalled() + vm.$emit('test3', 1, 2, 3, 4) + expect(spy.calls.count()).toBe(1) + }) + it('$once', () => { vm.$once('test', spy) vm.$emit('test', 1, 2, 3) diff --git a/types/vue.d.ts b/types/vue.d.ts index 7b5fdc23423..3ae8e02e998 100644 --- a/types/vue.d.ts +++ b/types/vue.d.ts @@ -61,7 +61,7 @@ export declare class Vue { ): (() => void); $on(event: string | string[], callback: Function): this; $once(event: string, callback: Function): this; - $off(event?: string, callback?: Function): this; + $off(event?: string | string[], callback?: Function): this; $emit(event: string, ...args: any[]): this; $nextTick(callback: (this: this) => void): void; $nextTick(): Promise;