-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
49 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
### Unreleased | ||
|
||
### 2.2.0 | ||
|
||
* Add support for Turbo from Hotwire - @excid3 | ||
* Remove dependency on Vue - @excid3 | ||
|
||
### 2.1.0 | ||
|
||
* See git history |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,42 @@ | ||
function handleVueDestruction(vue) { | ||
var turbolinksEvent = vue.$options.turbolinksDestroyEvent || 'turbolinks:visit'; | ||
document.addEventListener(turbolinksEvent, function teardown() { | ||
let event = vue.$options.destroyEvent || defaultEvent(); | ||
|
||
document.addEventListener(event, function teardown() { | ||
vue.$destroy(); | ||
document.removeEventListener(turbolinksEvent, teardown); | ||
document.removeEventListener(event, teardown); | ||
}); | ||
} | ||
|
||
var turbolinksAdapterMixin = { | ||
let 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 && this.$el) { | ||
handleVueDestruction(this); | ||
|
||
// cache original element | ||
this.$turbolinksCachedHTML = this.$el.outerHTML; | ||
this.$cachedHTML = this.$el.outerHTML; | ||
|
||
// register root hook to restore original element on destroy | ||
this.$once('hook:destroyed', function() { | ||
this.$el.outerHTML = this.$turbolinksCachedHTML | ||
this.$el.outerHTML = this.$cachedHTML | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
function plugin(Vue, options) { | ||
// Install a global mixin | ||
Vue.mixin(turbolinksAdapterMixin) | ||
Vue.mixin(Mixin) | ||
} | ||
|
||
function defaultEvent() { | ||
if (typeof Turbo !== 'undefined') { | ||
return "turbo:visit"; | ||
} else { | ||
return "turbolinks:visit"; | ||
} | ||
} | ||
|
||
export { turbolinksAdapterMixin }; | ||
export { Mixin }; | ||
export default plugin; |
This file contains 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