Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created February 24, 2023 13:03
Show Gist options
  • Save bennadel/d47e9479a65c6e46f8cb4c984b9c0d57 to your computer and use it in GitHub Desktop.
Save bennadel/d47e9479a65c6e46f8cb4c984b9c0d57 to your computer and use it in GitHub Desktop.
Creating Custom Turbo Stream Actions In Hotwire And Lucee CFML
<cfmodule template="./tags/page.cfm">
<cfoutput>
<h2>
Bouncing Back to Home
</h2>
<p>
<!--- Required for restoration visits - see note below. --->
<a href="index.htm">Go back to home</a> &rarr;
</p>
<!---
CAUTION: If the user returns to this page through a restoration visit (ie,
hitting the browser's BACK BUTTON), this Turbo-Stream element will no longer
be here since it is removed during the stream evaluation. As such, it is
important to provide the manual link above.
--
Also, by adding [data-action="replace"], we can override the current history
entry, somewhat preventing the back button problem.
--->
<turbo-stream
action="visit"
data-url="index.htm">
</turbo-stream>
</cfoutput>
</cfmodule>
<cfmodule template="./tags/page.cfm">
<cfoutput>
<h2>
Welcome to My Site
</h2>
<p>
<a href="bounce.htm">Go to Bouncer</a>
</p>
</cfoutput>
</cfmodule>
// Import core modules.
import * as Turbo from "@hotwired/turbo";
import { StreamActions } from "@hotwired/turbo";
// ----------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------- //
/**
* I support Turbo.visit() stream actions.
*/
StreamActions.visit = function() {
var url = this.dataset.url;
var action = ( this.dataset.action || "advance" );
var frame = ( this.dataset.frame || undefined );
Turbo.visit(
url,
{
action: action,
frame: frame
}
);
}
<turbo-stream
action="visit"
url="index.htm">
<!--- No content is relevant for this action. --->
</turbo-stream>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment