Skip to content
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

feat(core): add 'Vue.prototype.$notify' #7465

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions flow/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ declare interface Component {
$on: (event: string | Array<string>, fn: Function) => Component;
$once: (event: string, fn: Function) => Component;
$off: (event?: string | Array<string>, fn?: Function) => Component;
$notify: (event: string, ...args: Array<mixed>) => Array<mixed>;
$emit: (event: string, ...args: Array<mixed>) => Component;
$nextTick: (fn: Function) => void | Promise<*>;
$createElement: (tag?: string | Component, data?: Object, children?: VNodeChildren) => VNode;
Expand Down
13 changes: 10 additions & 3 deletions src/core/instance/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function eventsMixin (Vue: Class<Component>) {
return vm
}

Vue.prototype.$emit = function (event: string): Component {
Vue.prototype.$notify = function (event: string): Array<mixed> {
const vm: Component = this
if (process.env.NODE_ENV !== 'production') {
const lowerCaseEvent = event.toLowerCase()
Expand All @@ -126,17 +126,24 @@ export function eventsMixin (Vue: Class<Component>) {
}
}
let cbs = vm._events[event]
const retValues: Array<mixed> = []
if (cbs) {
cbs = cbs.length > 1 ? toArray(cbs) : cbs
const args = toArray(arguments, 1)
retValues.length = cbs.length
for (let i = 0, l = cbs.length; i < l; i++) {
try {
cbs[i].apply(vm, args)
retValues[i] = cbs[i].apply(vm, args)
} catch (e) {
handleError(e, vm, `event handler for "${event}"`)
}
}
}
return vm
return retValues
}

Vue.prototype.$emit = function (event: string): Component {
Vue.prototype.$notify.apply(this, arguments)
return this
}
}
32 changes: 32 additions & 0 deletions test/unit/features/directives/on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,38 @@ describe('Directive v-on', () => {
Vue.config.keyCodes = Object.create(null)
})

it('should return this for $emit', () => {
vm = new Vue({
el,
template: '<bar @custom="foo"></bar>',
methods: { foo: () => 123 },
components: {
bar: {
template: '<span>Hello</span>'
}
}
})
const retValue = vm.$children[0].$emit('custom', 'foo', 'bar')
expect(retValue).toBe(vm.$children[0])
})

it('should return an array for $notify', () => {
vm = new Vue({
el,
template: '<bar @custom="foo"></bar>',
methods: { foo: () => 123 },
components: {
bar: {
template: '<span>Hello</span>'
}
}
})
const retValue = vm.$children[0].$notify('custom', 'foo', 'bar')
expect(Array.isArray(retValue)).toBe(true)
expect(retValue.length).toBe(1)
expect(retValue[0]).toBe(123)
})

it('should bind to a child component', () => {
vm = new Vue({
el,
Expand Down
1 change: 1 addition & 0 deletions types/test/vue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Test extends Vue {
this.$once("", () => {});
this.$off("", () => {});
this.$emit("", 1, 2, 3);
this.$notify("", 1, 2, 3);
this.$nextTick(function() {
this.$nextTick;
});
Expand Down
1 change: 1 addition & 0 deletions types/vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface Vue {
$once(event: string, callback: Function): this;
$off(event?: string | string[], callback?: Function): this;
$emit(event: string, ...args: any[]): this;
$notify(event: string, ...args: any[]): any[];
$nextTick(callback: (this: this) => void): void;
$nextTick(): Promise<void>;
$createElement: CreateElement;
Expand Down