UNPKG

vue-events

Version:

Easy event handling for Vue applications.

151 lines (128 loc) 4.13 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; function plugin(Vue) { // Exit if the plugin has already been installed. if (plugin.installed) return; // Create a `vm` to serve as our global event bus. var events = new Vue({ methods: { /** * Emit the given event. * * @param {string|object} event * @param {...*} args */ emit: function emit(event) { for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } this.$emit.apply(this, [event].concat(args)); }, /** * Emit the given event. * * @param {string|object} event * @param {...*} args */ fire: function fire(event) { for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { args[_key2 - 1] = arguments[_key2]; } this.emit.apply(this, [event].concat(args)); }, /** * Listen for the given event. * * @param {string} event * @param {function} callback */ on: function on(event, callback) { this.$on(event, callback); }, /** * Listen for the given event. * * @param {string} event * @param {function} callback */ listen: function listen(event, callback) { this.on(event, callback); }, /** * Listen for the given event once. * * @param {string} event * @param {function} callback */ once: function once(event, callback) { this.$once(event, callback); }, /** * Remove one or more event listeners. * * @param {string} event * @param {function} callback */ off: function off(event, callback) { this.$off(event, callback); }, /** * Remove one or more event listeners. * * @param {string} event * @param {function} callback */ remove: function remove(event, callback) { this.off(event, callback); } } }); // Extend `Vue.prototype` to include our global event bus. Object.defineProperty(Vue.prototype, '$events', { get: function get() { return events; } }); // Register a mixin that adds an `events` option to Vue 2.0 components. Vue.mixin({ // Hook into the Vue 2.0 `beforeCreate` life-cycle event. beforeCreate: function beforeCreate() { // Exit if there's no `events` option. if (_typeof(this.$options.events) !== 'object') return; // Cache of events to bound functions for automatic unsubscriptions var eventMap = {}; // Loop through each event. for (var key in this.$options.events) { // Assign event type and bound function to map eventMap[key] = this.$options.events[key].bind(this); } // Listen for the `hook:beforeMount` Vue 2.0 life-cycle event. this.$once('hook:beforeMount', function () { // Loop through each event. for (var key in eventMap) { // Register a listener for the event. events.$on(key, eventMap[key]); } }); // Listen for the `hook:beforeDestroy` Vue 2.0 life-cycle event. this.$once('hook:beforeDestroy', function () { // Loop through each event. for (var key in eventMap) { // Register a listener for the event. events.$off(key, eventMap[key]); } // Release cache eventMap = null; }); } }); } // Check for `window.Vue` if (typeof window !== 'undefined' && window.Vue) { // Install plugin automatically. window.Vue.use(plugin); } exports.default = plugin;