From 5ccf57b48886f9c9b4303a2447ab1e9afb8df10f Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 12 Jan 2023 18:51:32 +0100 Subject: [PATCH] fix(js): use `on` instead of `addEventListener` to ensure cleanup --- .../controllers/replace_attachment_controller.ts | 2 +- app/javascript/controllers/support_controller.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/javascript/controllers/replace_attachment_controller.ts b/app/javascript/controllers/replace_attachment_controller.ts index 07dda54f4..cefe608cd 100644 --- a/app/javascript/controllers/replace_attachment_controller.ts +++ b/app/javascript/controllers/replace_attachment_controller.ts @@ -21,7 +21,7 @@ export class ReplaceAttachmentController extends ApplicationController { // reset autoAttachUrl which would add an attachment // when replace is not finalized - this.inputTarget.addEventListener('cancel', () => { + this.on(this.inputTarget, 'cancel', () => { this.inputTarget.dataset.autoAttachUrl = this.inputTarget.dataset.originalAutoAttachUrl; }); diff --git a/app/javascript/controllers/support_controller.ts b/app/javascript/controllers/support_controller.ts index d3d063bfd..22d947f0e 100644 --- a/app/javascript/controllers/support_controller.ts +++ b/app/javascript/controllers/support_controller.ts @@ -9,12 +9,12 @@ export class SupportController extends ApplicationController { connect() { this.inputRadioTargets.forEach((inputRadio) => { - inputRadio.addEventListener('change', this.onChange.bind(this)); - inputRadio.addEventListener('keydown', this.onChange.bind(this)); + this.on(inputRadio, 'change', this.onChange.bind(this)); + this.on(inputRadio, 'keydown', this.onChange.bind(this)); }); } - onChange(event: Event) { + private onChange(event: Event) { const target = event.target as HTMLInputElement; const content = this.getContentForTarget(target); @@ -29,12 +29,12 @@ export class SupportController extends ApplicationController { } } - getLabelForTarget(target: HTMLInputElement) { + private getLabelForTarget(target: HTMLInputElement) { const labelSelector = `label[for="${target.id}"]`; return document.querySelector(labelSelector); } - getContentForTarget(target: HTMLInputElement) { + private getContentForTarget(target: HTMLInputElement) { const label = this.getLabelForTarget(target); if (!label) { return null;