vue-eventbus2
Event Bus for Vue.js.
The Event Bus is a publish-subscribe pattern implementation to ease the communication between routines that doesn't know much about each other.
This Event Bus for Vue.js will act as an intermediary message broker between your different components. So you may tell it to do something from component A
, then you will "fire" that action in component B
, and C
, ...
Contents
Quickstart
npm install vue-eventbus2 --save
windowVue =windowEventBus =EventBusEventBusEventBus// > {some: "data"}EventBus// > another-event is triggered
API
Register Events Listeners
EventBus.listen(eventName |required, callback |required)
Use the listen
method to set event listeners:
EventBus;EventBus;
Firing Events
EventBus.fire(eventName |required, data |optional)
Use the fire
method to fire events:
EventBus;EventBus;EventBus;
Installation
Adding the package
npm install vue-eventbus2 --save# oryarn add vue-eventbus2 --save
Importing the package
1. Globally
The preferred usage of this package is to require it globally once:
windowVue = // vue-eventbus2 will look here or `global.Vue` to see where to find VuewindowEventBus =
2. Building
You may find it more convenient if you build the EventBus by passing the Vue instance your self:
let Vue =let EventBus = Vue
3. as a Plugin
You may prefer to use it as a Vue Plugin without exposing it to any global object, this way it will be associated to the main Vue instance which will be inherited to each Vue instance/component. read more about Vue Plugins.
You can import it as a plugin by requiring vue-eventbus2/plugin
, this will set the Event Bus as a $bus
property in the main Vue instance.
let Vue =let EventBusPlugin = ;Vue;let vm = ;vm$bus
4. as a Plugin with a custom property name
If you didn't like the $bus
property name, change it by passing the preferred name as an option:
let Vue =let EventBusPlugin = ;Vue; // passing the name as an optionlet vm = ;vmMyEventBus
Testing
this Event Bus shipped with the following built-in testing helpers.
Behind the scenes, the Event Bus only needs the
equal
assertion, it will check whether to use jasmine'sexpect(object).toEqual
or chai'sexpect(object).to.equal
and will act accordingly.
expectEvent(eventName)
Assert that the given event name is fired.
EventBusEventBus // PassEventBus // Fail..
notExpectEvent(eventName)
Assert that the given event name is not fired.
EventBusEventBus // FailEventBus // Pass..
expectListenEvent(eventName)
Assert that the given event name is being listened to.
EventBusEventBus // PassEventBus // Failed, no one is listening to the poor test2..
getFireHistory()
Get a list of the recorded fired events.
EventBusEventBusEventBus // ['one','two']
getListenHistory()
Get a list of the recorded fired events.
EventBusEventBusEventBus // ['one','two']
clearHistory()
Clears the recorded fire/listen events.
EventBus;EventBus;EventBus;EventBus;EventBus; // []EventBus; // []
Useful to be used within beforeEach
or afterEach
spec methods:
windowEventBus =;
fresh()
Alias for clearHistory except it will also return the EventBus instance.
Useful to be used within beforeEach
or afterEach
spec methods:
Contribution & Build Setup
# install dependenciesnpm install# run unit tests and watch file changesnpm run watch# run all tests and check code coveragenpm test
Credits
- Jeffrey Way for This lesson on laracasts.
- Webpack Template from Vue.js-Templates
License
The MIT License (MIT).