I’m learning stimulus and one detail that jumped to my mind was the differences between the API for handling targets (i.e. this.nameTarget) and the API for “state data” (this.data.get("index")).
It would seem to me that using a namespace for targets (this.targets.index or this.targets.get('index')) could have several advantages over the current implementation:
this.targets would allow lazy loading: targets would start “empty” and fetch each target when it is first used instead on fetching them all from the DOM on connection (as I am assuming is done now).
The previous point would allow removing the static targets variable from the controllers.
There would be a place to add common functions for targets. Iterate over all targets, for example.
The API would look more cohesive and, in my opinion, easier to learn.
Thanks a lot for the very detailed answer! Yes, I see how providing some default structure for shortcuts might be a good idea.
Why is the static targets property needed, then? Why can’t this.targets.find("foo") directly query the DOM for [data-target="my-controller.foo"] the first time it is called?
That’s exactly what happens under the hood. No DOM queries are performed until you access a target property. Accessing this.fooTarget calls this.targets.find("foo") and returns the result every time.