From 673ab4c3302e5149a6b6b65039ee52176fa5f8d7 Mon Sep 17 00:00:00 2001 From: Nathan Hopkins Date: Fri, 4 Oct 2019 07:11:13 -0600 Subject: [PATCH] Preserve `this` context on lifecycle methods --- javascript/stimulus_reflex.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/javascript/stimulus_reflex.js b/javascript/stimulus_reflex.js index 6c47ad01..62397310 100644 --- a/javascript/stimulus_reflex.js +++ b/javascript/stimulus_reflex.js @@ -120,11 +120,19 @@ const invokeLifecycleMethod = (stage, reflex, element) => { const genericLifecycleMethod = controller[genericLifecycleMethodName] if (typeof specificLifecycleMethod === 'function') { - setTimeout(() => specificLifecycleMethod(element, element.reflexError), 1) + setTimeout( + () => + specificLifecycleMethod.call(controller, element, element.reflexError), + 1 + ) } if (typeof genericLifecycleMethod === 'function') { - setTimeout(() => genericLifecycleMethod(element, element.reflexError), 1) + setTimeout( + () => + genericLifecycleMethod.call(controller, element, element.reflexError), + 1 + ) } }