Skip to content

support $off multi event #5056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flow/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ declare interface Component {
$watch: (expOrFn: string | Function, cb: Function, options?: Object) => Function;
$on: (event: string | Array<string>, fn: Function) => Component;
$once: (event: string, fn: Function) => Component;
$off: (event?: string, fn?: Function) => Component;
$off: (event?: string | Array<string>, fn?: Function) => Component;
$emit: (event: string, ...args: Array<mixed>) => Component;
$nextTick: (fn: Function) => void;
$createElement: (tag?: string | Component, data?: Object, children?: VNodeChildren) => VNode;
Expand Down
9 changes: 8 additions & 1 deletion src/core/instance/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,20 @@ export function eventsMixin (Vue: Class<Component>) {
return vm
}

Vue.prototype.$off = function (event?: string, fn?: Function): Component {
Vue.prototype.$off = function (event?: string | Array<string>, 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) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/features/instance/methods-events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion types/vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
Expand Down