Skip to content

Instantly share code, notes, and snippets.

@pocheptsov
Last active December 9, 2022 23:24
Show Gist options
  • Save pocheptsov/ea2aac13a20f394e371eeb14042449b4 to your computer and use it in GitHub Desktop.
Save pocheptsov/ea2aac13a20f394e371eeb14042449b4 to your computer and use it in GitHub Desktop.
Hotwired Turbo and partial page update patch
import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static values = {
userId: String,
data: Object,
}
dataValue: object
userIdValue: string
connect() {
window.Appcues.identify(this.userIdValue, this.dataValue)
document.addEventListener('turbo:load', this.initPage)
}
disconnect() {
document.removeEventListener('turbo:load', this.initPage)
}
initPage() {
if (typeof window.Appcues === 'object') {
window.Appcues.page()
}
}
}
// https://github.com/hotwired/turbo/issues/499#issuecomment-996751695
import { PageRenderer } from '@hotwired/turbo'
Object.assign(PageRenderer.prototype, {
assignNewBody() {
const container = document.querySelector('#app')
const newContainer = this.newElement.querySelector('#app')
if (container && newContainer) {
container.replaceWith(newContainer)
} else {
PageRenderer.renderElement(this.currentElement, this.newElement)
}
},
})
<!DOCTYPE html>
<html>
<body>
<div id="app">
</div>
<div
data-controller="appcues"
data-appcues-user-id-value="<%= @current_user.id.to_s %>"
data-appcues-data-value="<%= appcues_data.to_json %>"
>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment