Last active
December 9, 2022 23:24
-
-
Save pocheptsov/ea2aac13a20f394e371eeb14042449b4 to your computer and use it in GitHub Desktop.
Hotwired Turbo and partial page update patch
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
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() | |
} | |
} | |
} |
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
// 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) | |
} | |
}, | |
}) |
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
<!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