I just wanted to drop this here since being able to create a custom stream action helped me figure out how to handle a transcluded Form inside another page:
Coming from a ColdFusion background, most form POSTs are followed by a 302 redirect with a Location header. However, if I use a Location inside a <turbo-frame>, then I can only update the contents inside that frame, which can be problematic if I want to update anything outside that frame (as well).
I know that most people in Hotwire will use Streams to perform append and replace actions. But, as a baby-step, I wanted to use a custom visit action to tell the parent page to reload two different frames. This incurs more network latency, obviously; but, is a relatively straightforward mental model coming from ColdFusion.
I find that this does the same, but requires less extending in the front end. I usually make a helper in my backend that takes multiple streams and creates a response.
I’m not sure i understand what states an expandable list would have that wouldn’t work with this approach. I would let my HTML hold the state by removing/adding the content when necessary.
<div id="expandable-list">
<div>
This is the content that will always be shown
</div>
<div id="expanded-list-content">
this can be replaced when expanding/collapsing
</div>
</div>
If the amount of content is very small I would probably not bother using streams at all and just use styling to hide to the content when collapsing.
Side-note: I strongly prefer to have my logic in the backend and as a result i prefer to restructure my HTML so that I can use turbo without stimulus unless i specifically need something that enhances the user experience. You may not feel the same and that’s totally valid as well
Check if 1) the version of Turbo you are running >= 7.2.0, 2) StreamActions is defined (not Turbo.StreamActions.) or 3) if you have an error importing the module, like:
Uncaught TypeError: Failed to resolve module specifier “@hotwired/turbo”…
If StreamActions is undefined, use Turbo.StreamActions.
- import {StreamActions} from "@hotwired/turbo";
console.log("Before")
- console.log(StreamActions);
+ console.log(Turbo.StreamActions);
- StreamActions.console_log = function () {
+ Turbo.StreamActions.console_log = function () {
console.log("CONSOLE LOGGING");
}
- StreamActions.toast = function () {
+ Turbo.StreamActions.toast = function () {
console.log("TOASTER");
}
console.log("After");
- console.log(StreamActions);
+ console.log(Turbo.StreamActions);
or fix the import module, something like the following might resolve your issue, depending on how your using the module:
- import {StreamActions} from "@hotwired/turbo";
+ import { StreamActions } from 'https://cdn.skypack.dev/@hotwired/turbo';