bootstrap-vue
Version:
BootstrapVue provides one of the most comprehensive implementations of Bootstrap 4 components and grid system for Vue.js and with extensive and automated WAI-ARIA accessibility markup.
68 lines (59 loc) • 2.3 kB
JavaScript
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
import { isArray } from '../utils/array';
/**
* Issue #569: collapse::toggle::state triggered too many times
* @link https://github.com/bootstrap-vue/bootstrap-vue/issues/569
*/
var BVRL = '__BV_root_listeners__';
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.
*
* This adds a non-reactive prop to a vm on the fly
* in order to avoid object observation and its performance costs
* to something that needs no reactivity.
* It should be highly unlikely there are any naming collisions.
* @param {string} event
* @param {function} callback
* @chainable
*/
listenOnRoot: function listenOnRoot(event, callback) {
if (!this[BVRL] || !isArray(this[BVRL])) {
this[BVRL] = [];
}
this[BVRL].push({ event: event, callback: callback });
this.$root.$on(event, callback);
return this;
},
/**
* Convenience method for calling vm.$emit on vm.$root.
* @param {string} event
* @param {*} args
* @chainable
*/
emitOnRoot: function emitOnRoot(event) {
var _$root;
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
(_$root = this.$root).$emit.apply(_$root, [event].concat(_toConsumableArray(args)));
return this;
}
},
beforeDestroy: function beforeDestroy() {
if (this[BVRL] && isArray(this[BVRL])) {
while (this[BVRL].length > 0) {
// shift to process in order
var _BVRL$shift = this[BVRL].shift(),
event = _BVRL$shift.event,
callback = _BVRL$shift.callback;
this.$root.$off(event, callback);
}
}
}
};