forked from jeffreyguenther/vue-turbolinks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (27 loc) · 951 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function handleVueDestructionOn(turbolinksEvent, vue) {
document.addEventListener(turbolinksEvent, function teardown() {
vue.$destroy();
document.removeEventListener(turbolinksEvent, teardown);
});
}
function plugin(Vue, options) {
// Install a global mixin
Vue.mixin({
beforeMount: function() {
// If this is the root component, we want to cache the original element contents to replace later
// We don't care about sub-components, just the root
if (this == this.$root) {
var destroyEvent = this.$options.turbolinksDestroyEvent || 'turbolinks:before-render'
handleVueDestructionOn(destroyEvent, this);
this.$originalEl = this.$el.outerHTML;
}
},
destroyed: function() {
// We only need to revert the html for the root component
if (this == this.$root && this.$el) {
this.$el.outerHTML = this.$originalEl;
}
}
})
};
export default plugin;