Events
Register Global Events in Your Markup
by @julianrubisch
Bad
connect() {
document.addEventListener(...);
}
Good
<div data-controller="gallery" data-action="[email protected]>gallery#layout">...</div>
layout(e) {
...
}
Rationale
Stimulus will handle the adding and removing of event listeners.
Contraindications
The controller code makes assumption about the markup, which may not be feasible if you’re creating a Stimulus controller library. In this case, be sure to unregister your event listeners in disconnect
.