-
Notifications
You must be signed in to change notification settings - Fork 443
Dynamically mounting an action to Stimulus #478
Description
Hey!
First of all, I am a big fan of Stimulus 🎉
Recently, I found myself missing a way to dynamically adding actions. Is there a way to do so? Or, perhaps, there only be a way to do it eventually?
Use case
Handling a dropdown menu via JS, for example. Currently, I am collapsing such menus via something like click@window->dropdown#hide which is not ideal: it adds a lot of unnecessary events, fills up the debug mode and it only gets worse if there are more such dropdowns.
Obviously, it is possible add window.addEventListener(...), however, it becomes "detached" from the controller. So it would be nice to keep things consistent.
Expected functionality
Perhaps having an exposed Action class and/or mounting method could be an option.
Referring to the above mentioned use case, it would be possible to mount a new action when the dropdown is opened: new Action(element, index, descriptor) and then destroy this action upon disconnect.
P.s.
Not sure if this is a question or a feature request. I have tried searching for an answer, however, with no luck. Also, I am not that sure if this is a bad practice and/or is inconsistent with the Stimulus codebase or the way it is mounted.