Created
November 19, 2017 22:56
-
-
Save darklow/35c82b9667a6810d4fb69d45f40394b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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