First of all, StimulusJS is fantastic and I love everyone who has worked on it.
That said, I’ve spent the last two hours banging my head against the wall trying to figure this out, and I finally made a couple of JS Fiddles to share with the group here and get some much-needed help.
Background
I have two general-purpose controllers, an addable controller and a removable controller. The removable controller just allows anything within it to call removable#remove in an action and delete the element. The addable controller takes some content in a template (think, a form), and copies it onto the page.
Exhibit A Here’s the first JS Fiddle. You can only add one new box at a time. I control this by disabling the button when a new box is present, and then re-enabling the button when the box is removed. Note line 23, where I’m calling two actions, one to remove the element and one to re-enable the button. But note that when you add and then remove a box, the button stays disabled and only the removable#remove action is called (check the console logs)!
Exhibit B Here’s the same JS Fiddle, slightly modified. The only thing I’ve changed is that I have removed the call to removable#remove on line 23, and now the action to re-enable the button is called!
I guess I’m just confused as to why this JS Fiddle functions as intended, then. Here, all I’ve done is allowed the initial box to trigger both removable#remove and addable#boxRemoved. If you first click the “Add Another Box” button, then remove the initial box element, note that is is both removed and the button is re-enabled.
If removing an element uninstalls all of the actions, how is this newly-modified original box element executing both actions?
I haven’t looked too closely, but I think there are subtle timing issues at play with nested controllers and dynamically added / removed content (Stimulus uses MutationObserver under the hood to detect when elements are added and removed).