bootstrap-vue
Version:
BootstrapVue, with more than 85 custom components, over 45 plugins, several custom directives, and over 300 icons, provides one of the most comprehensive implementations of Bootstrap v4 components and grid system for Vue.js. With extensive and automated W
57 lines (54 loc) • 1.84 kB
JavaScript
// @vue/component
export default {
methods: {
/**
* Safely register event listeners on the root Vue node
* While Vue automatically removes listeners for individual components,
* when a component registers a listener on root and is destroyed,
* this orphans a callback because the node is gone,
* but the root does not clear the callback
*
* When registering a `$root` listener, it also registers a listener on
* the component's `beforeDestroy()` hook to automatically remove the
* event listener from the `$root` instance
*
* @param {string} event
* @param {function} callback
*/
listenOnRoot(event, callback) {
this.$root.$on(event, callback)
this.$on('hook:beforeDestroy', () => {
this.$root.$off(event, callback)
})
},
/**
* Safely register a `$once()` event listener on the root Vue node
* While Vue automatically removes listeners for individual components,
* when a component registers a listener on root and is destroyed,
* this orphans a callback because the node is gone,
* but the root does not clear the callback
*
* When registering a $root listener, it also registers a listener on
* the component's `beforeDestroy` hook to automatically remove the
* event listener from the $root instance.
*
* @param {string} event
* @param {function} callback
*/
listenOnRootOnce(event, callback) {
this.$root.$once(event, callback)
this.$on('hook:beforeDestroy', () => {
this.$root.$off(event, callback)
})
},
/**
* Convenience method for calling `vm.$emit()` on `vm.$root`
*
* @param {string} event
* @param {*} args
*/
emitOnRoot(event, ...args) {
this.$root.$emit(event, ...args)
}
}
}