Problem
I find myself writing, in various forms, controller code that allows some action to be dispatched when a controller connects.
This method could be manually called in the connect method, but sometimes it's more appropriate to have this call made optionally depending on usage. We could add a value to 'do thing on init', but writing an additional action feels more appropriate and makes more sense.
Example
As its simplest, the code often looks like this...
export class extends Controller {
connect() {
// Do some kind of setup
this.dispatch('start', { bubbles: false, cancelable: false });
}
}
<div data-controller="my-controller" data-action="my-controller:start->my-controller#doSomething:once">
...STUFF
</div>
Proposal
Every controller, when connected would dispatch two events on the controlled element by default. These two events would bubble, are not cancelable and their prefix can be configured using the Stimulus schema.
init:my-controller - dispatched before the connect method callback is called. Less critical.
ready:my-controller - dispatched after the connect method has completed. If the connect method is Async /returns a promise it will only dispatch when that has resolved successfully.
Note: Originally, I had suggested these events should not bubble but it may make sense to have this bubble and allow them to be restricted by the self event filter.
This also sets up a way to do a global event listener, per controller identifier, to hide something until ready. E.g. a data attribute or class that gets removed when the ready event is dispatched.
Example new usage
We no longer need the overhead in our controller...
<div data-controller="my-controller" data-action="ready:my-controller->my-controller#doSomething:self:once">
...STUFF
</div>
Additional references
Problem
I find myself writing, in various forms, controller code that allows some action to be dispatched when a controller connects.
This method could be manually called in the connect method, but sometimes it's more appropriate to have this call made optionally depending on usage. We could add a value to 'do thing on init', but writing an additional action feels more appropriate and makes more sense.
Example
As its simplest, the code often looks like this...
Proposal
Every controller, when connected would dispatch two events on the controlled element by default. These two events would bubble, are not cancelable and their prefix can be configured using the Stimulus schema.
init:my-controller- dispatched before the connect method callback is called. Less critical.ready:my-controller- dispatched after the connect method has completed. If the connect method is Async /returns a promise it will only dispatch when that has resolved successfully.Note: Originally, I had suggested these events should not bubble but it may make sense to have this bubble and allow them to be restricted by the
selfevent filter.This also sets up a way to do a global event listener, per controller identifier, to hide something until ready. E.g. a data attribute or class that gets removed when the ready event is dispatched.
Example new usage
We no longer need the overhead in our controller...
Additional references
inithttps://alpinejs.dev/essentials/lifecycleloadtrigger https://htmx.org/attributes/hx-trigger/