Going Generic
In our case, what do generic Stimulus controllers look like?
What we have now is one controller that responds to a click by showing or hiding a target element and by changing the text of a target.
Let’s think a bit more abstractly, and what we have here are two separate actions that respond to a click: one that adds and removes a CSS class and one that changes the text of a button. we’re going to split those into two separate controllers to show how Stimulus allows for composition of small, generic pieces of action.
Now, there’s an obvious downside here, which is that the code and the HTML are going to get more verbose. Let’s look at the code and then talk about the upsides.
Both the generic Stimulus controllers are mostly subsets ...
Get Modern Front-End Development for Rails now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.