feat(turbo): add on method to stimulus application controller

This commit is contained in:
Paul Chavard 2022-05-04 12:42:42 +02:00
parent 2eaf486372
commit cfb83bbdda

View file

@ -22,4 +22,19 @@ export class ApplicationController extends Controller {
target: document.documentElement
});
}
protected on<HandlerEvent extends Event = Event>(
eventName: string,
handler: (event: HandlerEvent) => void
): void {
const disconnect = this.disconnect;
const callback = (event: Event): void => {
handler(event as HandlerEvent);
};
this.element.addEventListener(eventName, callback);
this.disconnect = () => {
this.element.removeEventListener(eventName, callback);
disconnect.call(this);
};
}
}