Skip to content

Instantly share code, notes, and snippets.

@darklow
Created November 19, 2017 22:56
Show Gist options
  • Save darklow/35c82b9667a6810d4fb69d45f40394b6 to your computer and use it in GitHub Desktop.
Save darklow/35c82b9667a6810d4fb69d45f40394b6 to your computer and use it in GitHub Desktop.
import Vue from 'vue'
// Event proxy/bus for sharing data across non-parent child components
// https://vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication
const event = {
bus: null,
init() {
if (!this.bus) {
this.bus = new Vue();
}
return this;
},
emit(name, ...args) {
this.bus.$emit(name, ...args);
return this;
},
on() {
if (arguments.length === 2) {
this.bus.$on(arguments[0], arguments[1]);
} else {
Object.keys(arguments[0]).forEach(key => {
this.bus.$on(key, arguments[0][key]);
});
}
return this;
},
off() {
this.bus.$off(...arguments)
}
}
export {event};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment