|
5 | 5 | import EventManager from './EventManager'; |
6 | 6 | import { forEach, isArray, isObject } from './util'; |
7 | 7 |
|
8 | | -function plugin(Vue) { |
| 8 | +var Events = new EventManager(); |
9 | 9 |
|
10 | | - if (plugin.installed) { |
| 10 | +Events.install = function (Vue) { |
| 11 | + |
| 12 | + if (this.installed) { |
11 | 13 | return; |
12 | 14 | } |
13 | 15 |
|
14 | | - var Events = new EventManager(); |
15 | | - var version = Number(Vue.version.split('.')[0]); |
| 16 | + Vue.events = this; |
| 17 | + Vue.prototype.$events = this; |
| 18 | + Vue.prototype.$trigger = this.trigger.bind(this); |
| 19 | + Vue.mixin(Number(Vue.version[0]) < 2 ? {init: initEvents} : {beforeCreate: initEvents}); |
| 20 | +}; |
16 | 21 |
|
17 | | - function initEvents() { |
| 22 | +function initEvents() { |
18 | 23 |
|
19 | | - var {events} = this.$options, _events = []; |
| 24 | + var {events} = this.$options, _events = []; |
20 | 25 |
|
21 | | - if (events) { |
| 26 | + if (events) { |
22 | 27 |
|
23 | | - forEach(events, (listeners, event) => { |
24 | | - forEach(isArray(listeners) ? listeners : [listeners], listener => { |
| 28 | + forEach(events, (listeners, event) => { |
| 29 | + forEach(isArray(listeners) ? listeners : [listeners], listener => { |
25 | 30 |
|
26 | | - var priority = 0; |
| 31 | + var priority = 0; |
27 | 32 |
|
28 | | - if (isObject(listener)) { |
29 | | - priority = listener.priority; |
30 | | - listener = listener.handler; |
31 | | - } |
| 33 | + if (isObject(listener)) { |
| 34 | + priority = listener.priority; |
| 35 | + listener = listener.handler; |
| 36 | + } |
32 | 37 |
|
33 | | - _events.push(Events.on(event, listener.bind(this), priority)); |
34 | | - }); |
| 38 | + _events.push(Events.on(event, listener.bind(this), priority)); |
35 | 39 | }); |
| 40 | + }); |
36 | 41 |
|
37 | | - this.$on('hook:beforeDestroy', () => _events.forEach(off => off())); |
38 | | - } |
| 42 | + this.$on('hook:beforeDestroy', () => _events.forEach(off => off())); |
39 | 43 | } |
40 | | - |
41 | | - Vue.events = Events; |
42 | | - Vue.prototype.$events = Events; |
43 | | - Vue.prototype.$trigger = Events.trigger.bind(Events); |
44 | | - Vue.mixin(version < 2 ? {init: initEvents} : {beforeCreate: initEvents}); |
45 | 44 | } |
46 | 45 |
|
47 | 46 | if (typeof window !== 'undefined' && window.Vue) { |
48 | | - window.Vue.use(plugin); |
| 47 | + window.Vue.use(Events); |
49 | 48 | } |
50 | 49 |
|
51 | | -export default plugin; |
| 50 | +export default Events; |
0 commit comments