From 443d21f0bc650edd80bf37b9e475ab1b5c04f699 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 16 Nov 2023 23:04:24 +0100 Subject: [PATCH] Optimize events on handler By storing the targets only once per base node we spare some unneccessary dom queries. --- app/javascript/alchemy_admin/utils/events.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/javascript/alchemy_admin/utils/events.js b/app/javascript/alchemy_admin/utils/events.js index 8d085ae993..933d8d5b85 100644 --- a/app/javascript/alchemy_admin/utils/events.js +++ b/app/javascript/alchemy_admin/utils/events.js @@ -1,7 +1,8 @@ export function on(eventName, baseSelector, targetSelector, callback) { document.querySelectorAll(baseSelector).forEach((baseNode) => { + const targets = Array.from(baseNode.querySelectorAll(targetSelector)) + baseNode.addEventListener(eventName, (evt) => { - const targets = Array.from(baseNode.querySelectorAll(targetSelector)) let currentNode = evt.target while (currentNode !== baseNode) {