Stimulus Suggestion: Controller Aliases

I would love the ability to alias a controller name in Stimulus. Here’s a possible interface.

<div data-controller="loader->product-loader" data-product-loader-url-value="https://...">
  <div data-product-loader-target="spinner">...</div>
  <div data-product-loader-target="result">...</div>
</div>

There are several reasons for doing this.

Conflicting Controllers

There are times you need multiple versions of the same controller. Perhaps a product-loader and a category-loader in the same area that are both loader controllers. Aliases would allow two versions of the same controller to overlap without conflict.

Self-Documenting

I usually keep controllers generic, however it can be difficult to determine what each controller does in a sea of generic controller names. What is this loader for? It’s loading a product.

Shorten Controller Names

There are times generic controllers won’t work and you need something specific to the domain. I like to use namespaces (folders) to keep this organized, however that quickly becomes unwiedly.

For example, if you have a controller name store--products--category-loader you could alias it to category-loader and reference it easier throughout the rest of the HTML.

For the record, there was an earlier discussion on this topic, but it didn’t get much activity.

Would anyone else find aliasing controllers useful?

6 Likes

Not something we need, but not to say others don’t. We don’t experience any of the issues listed above.

Agree on the namespaced controllers like admin--toogle that I would alias as toggle

Perahps @ would be a better separator. It stands out more when you have a lot of dashes.

<div data-controller="store--products--category-loader@loader"></div>

In the past, when I tried to create a counter with a recursive structure, it did not work because of the scope.
At that time I wanted a little alias functionality.

However, 3.2 was released.
I think I would use outlets to solve this problem these days.

I came across a need for something like this just this week - there’s no way to isolate specific targets for specific controllers when there are multiple of the same controller within the same hierarchy.

Either aliases or being able to specify the target designator for each controller perhaps ?