Stimulus Discourse

Controllers: components or functionality?

I’m in the process of implementing Stimulus to handle AJAX requests on a Hanami project. Something that I keep batting back and forth on is whether controllers should be there to handle small, specific bits of functionality, or to essentially turn an element into a component.

The reason I began implementing them as though they were components is because it allowed be to add a load function to each controller, meaning that tables/panels/sections are capable of loading themselves. This makes it really easy to replace sections of the page upon a successful AJAX request, and has the nice side effect of simplifying controllers and views which have a lot going on within them.

In the example I’m currently working on, there is a panel which displays a list of a certain resource from my database, lets you create a new one, or update/delete existing ones. Each form contains a “privateKey” which I’ve set as a data-target, meaning that if I want this panel/controller to act as a component, then I have to cycle through all of the this.privateKeyTargets to find the one relevant to the current action.

Alternatively, if there were a controller for each of the actions (create, update and delete), then this wouldn’t be a problem. But it also wouldn’t be quite as easy to reload the appropriate components, and would most likely mean using more inner/outer controllers.

Either of these approaches would work but I’m wondering which is closer to the intention of the developers? And does anybody have any particular thoughts or insights around any of this?