-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventsMixin.js
88 lines (71 loc) · 1.68 KB
/
eventsMixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Vue from './Vue';
Vue.prototype.$on = function (event, fn) {
let vm = this;
if (Array.isArray(event)) {
for (let i = 0, l = event.length; i < l; i++) {
this.$on(evnet[i], fn);
}
} else {
(vm._events[event] || (vm._events[event] = [])).push(fn);
}
return vm;
}
Vue.prototype.$off = function (event, fn) {
let vm = this;
if (!arguments.length) {
vm._events = Object.create(null);
return vm;
}
if (Array.isArray(event)) {
for (let i = 0, l = event.length; i < l; i++) {
this.$off(evnet[i], fn);
}
return vm;
}
let cbs = vm._events[event];
if (!cbs) {
return vm;
}
if (arguments.length === 1) {
vm._events[event] = Object.create(null);
return vm;
}
if (fn) {
const cbs = vm._events[event];
let cb;
let i = cbs.length;
while (i--) {
cb = cbs[i];
if (cb === fn || cb.fn === fn) {
cbs.splice(i, 1);
break;
}
}
}
return vm;
}
Vue.prototype.$once = function (event, fn) {
let vm = this;
function on() {
vm.$off(event, fn);
fn.apply(vm, arguments);
}
on.fn = fn;
vm.$on(on, fn);
return vm;
}
Vue.prototype.$emit = function (event) {
let vm = this
let args = Array.prototype.slice.call(arguments, 1);
let cbs = vm._events[event];
if (cbs) {
for (let i = 0, l = cbs.length; i < l; i++) {
try {
cbs[i].apply(vm, args);
} catch (e) {
console.error(e);
}
}
}
return vm;
}