Skip to content

Commit 7bf739b

Browse files
author
kingwl
committed
support $on multi event
1 parent e853d19 commit 7bf739b

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

flow/component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ declare interface Component {
3636
$set: (obj: Array<mixed> | Object, key: mixed, val: mixed) => void;
3737
$delete: (obj: Object, key: string) => void;
3838
$watch: (expOrFn: string | Function, cb: Function, options?: Object) => Function;
39-
$on: (event: string, fn: Function) => Component;
39+
$on: (event: string | Array<string>, fn: Function) => Component;
4040
$once: (event: string, fn: Function) => Component;
4141
$off: (event?: string, fn?: Function) => Component;
4242
$emit: (event: string, ...args: Array<mixed>) => Component;

src/core/instance/events.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,19 @@ export function updateComponentListeners (
3838

3939
export function eventsMixin (Vue: Class<Component>) {
4040
const hookRE = /^hook:/
41-
Vue.prototype.$on = function (event: string, fn: Function): Component {
41+
Vue.prototype.$on = function (event: string | Array<string>, fn: Function): Component {
4242
const vm: Component = this
43-
;(vm._events[event] || (vm._events[event] = [])).push(fn)
44-
// optimize hook:event cost by using a boolean flag marked at registration
45-
// instead of a hash lookup
46-
if (hookRE.test(event)) {
47-
vm._hasHookEvent = true
43+
if (Array.isArray(event)) {
44+
for (let i = 0, l = event.length; i < l; i++) {
45+
this.$on(event[i], fn)
46+
}
47+
} else {
48+
(vm._events[event] || (vm._events[event] = [])).push(fn)
49+
// optimize hook:event cost by using a boolean flag marked at registration
50+
// instead of a hash lookup
51+
if (hookRE.test(event)) {
52+
vm._hasHookEvent = true
53+
}
4854
}
4955
return vm
5056
}

test/unit/features/instance/methods-events.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ describe('Instance methods events', () => {
1818
expect(spy).toHaveBeenCalledWith(1, 2, 3, 4)
1919
})
2020

21+
it('$on multi event', () => {
22+
vm.$on(['test1', 'test2'], function () {
23+
expect(this).toBe(vm)
24+
spy.apply(this, arguments)
25+
})
26+
vm.$emit('test1', 1, 2, 3, 4)
27+
expect(spy.calls.count()).toBe(1)
28+
expect(spy).toHaveBeenCalledWith(1, 2, 3, 4)
29+
vm.$emit('test2', 5, 6, 7, 8)
30+
expect(spy.calls.count()).toBe(2)
31+
expect(spy).toHaveBeenCalledWith(5, 6, 7, 8)
32+
})
33+
2134
it('$once', () => {
2235
vm.$once('test', spy)
2336
vm.$emit('test', 1, 2, 3)

types/vue.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export declare class Vue {
5353
callback: WatchHandler<this>,
5454
options?: WatchOptions
5555
): (() => void);
56-
$on(event: string, callback: Function): this;
56+
$on(event: string | string[], callback: Function): this;
5757
$once(event: string, callback: Function): this;
5858
$off(event?: string, callback?: Function): this;
5959
$emit(event: string, ...args: any[]): this;

0 commit comments

Comments
 (0)