├── LICENSE ├── README.md └── vue-bus.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 fffixed 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vue-bus 2 | [![awesome-vue](https://img.shields.io/badge/Vue.js-AWESOME-ff69b4.svg)](//github.com/vuejs/awesome-vue) 3 | [![license](https://img.shields.io/github/license/fffixed/vue-bus.svg)](//opensource.org/licenses/MIT) 4 | 5 | A tiny simple central event bus plugin for [Vue.js](//vuejs.org) (requires Vue >= 2.0). 6 | 7 | The plugin realise [Non Parent-Child Communication](//vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication). 8 | 9 | _(655 byte gzip)_ 10 | 11 | ## Installation 12 | Download and use with your build system 13 | ```javascript 14 | import VueBus from 'vue-bus' 15 | // ... maybe ... 16 | var VueBus = require('vue-bus') 17 | 18 | // ... and ... 19 | 20 | Vue.use(VueBus) 21 | ``` 22 | Or just include it with a script tag 23 | ```html 24 | 25 | ``` 26 | :sparkles: 27 | 28 | ## Usage 29 | direct way: 30 | ```javascript 31 | // in component A's method 32 | this.$bus.$emit('my-event', 1) 33 | 34 | // in component B's created hook 35 | this.$bus.$on('my-event', function(arg) { 36 | // ... 37 | }) 38 | 39 | //And don't forget to use "this.$bus.$off" to remove unnecessary listeners. 40 | ``` 41 | 42 | magic way: 43 | ```javascript 44 | // in component A's method 45 | this.$bus=['my-event', 1] 46 | 47 | // in component B create $bus option 48 | methods: { /* ... */ }, 49 | $bus: { 50 | 'my-event': function(arg) { 51 | // ... 52 | } 53 | } 54 | ``` 55 | 56 | ## License 57 | [MIT](//opensource.org/licenses/MIT) 58 | 59 | Copyright (c) 2017 fffixed 60 | -------------------------------------------------------------------------------- /vue-bus.js: -------------------------------------------------------------------------------- 1 | //********************************************* 2 | // vue-bus v0.9.2 3 | // (c) 2017 fffixed 4 | //********************************************* 5 | ;(function() { 6 | 7 | 8 | var vueBus = {} 9 | 10 | vueBus.install = function (Vue) { 11 | var version = Number(Vue.version.split('.')[0]) 12 | if (version < 2) return //check Vue version 13 | 14 | var bus = new Vue() 15 | 16 | Object.defineProperty(Vue.prototype, '$bus', { //for "this.$bus" 17 | get: function () { return bus }, 18 | set: function (evt) { //alt way to send an event (this.$bus=['event_name',arg1,arg2]) 19 | if (typeof evt === 'string') evt = [evt] 20 | bus.$emit.apply(bus, evt) 21 | } 22 | }) 23 | 24 | Vue.mixin({ 25 | created: function () { //add option "$bus" instead bus.$on in created hook 26 | var $bus = this.$options.$bus 27 | this.$busListeners = {} 28 | for (var name in $bus) { 29 | this.$busListeners[name] = $bus[name].bind(this) //rebind and remember each declared listener 30 | bus.$on(name, this.$busListeners[name]) //register a listener for the event 31 | } 32 | }, 33 | beforeDestroy: function () { //unreg listeners 34 | for (var name in this.$busListeners) bus.$off(name, this.$busListeners[name]) 35 | this.$busListeners = null 36 | } 37 | }) 38 | } 39 | 40 | 41 | // if module 42 | if (typeof exports === 'object') { module.exports = vueBus; return } 43 | if (typeof define === 'function' && define.amd) { define([], function(){ return vueBus }); return } 44 | 45 | // if direct include 46 | if (typeof window !== 'undefined' && window.Vue) { 47 | window.VueBus = vueBus 48 | window.Vue.use(vueBus) //auto-activation 49 | } 50 | 51 | 52 | })() 53 | --------------------------------------------------------------------------------