Allow me to wax philosophical for a moment (as I try to build-up a better mental model for Hotwire).
I come from the Angular world (since AngularJS 1.0.8). That’s a decade of “Services” that can all be injected (Inversion of Control, IoC) into other services, controllers, and directives. As such, the idea of encapsulating complexity behind a service is my default thought.
When I started to dig into Hotwire Stimulus, one of my first questions was therefore: How do I apply dependency injection (DI) with Stimulus controllers if the constructor is not something I have control over?
The more I dig into Hotwire, however, and the more I think about the “Hotwire way”, I wonder if — in a Stimulus context — the desire to have IoC is a bit of a code smell?
Here’s my reasoning: in a traditional SPA (Single Page Application), much of the logic — and therefore, the complexity — lives on the client-side. As such, in order to make the complexity easier to manage, we push it all into services and then provide those services wherever they are needed. In Hotwire, we’re supposed to be taking that complexity and moving it back onto the server. As such, many of the services that I might have had in an Angular app should be replaced with some sort of an HTTP request to a server-rendered template that either replaces the current page in some totality or some targeted Turbo-Frame (or DOM node via Streams).
To make this thought more concrete, one of the first “services” that I wanted to “inject” was an API Client. But, what I now realize is that I probably don’t need an API client, I just need to use Turbo.visit()
. Because, after all, I don’t want to parse and translate JSON — I just want to grab some HTML over the wire and jam it into the DOM.
Does that connect at all? I’m mostly just talking out loud here; but, if anyone has any feedback, I’d love to hear it. Thanks!