From 25b269977802d791103d59ed6eb971f63808ecbc Mon Sep 17 00:00:00 2001 From: Marco Roth Date: Sat, 20 Mar 2021 02:51:57 +0100 Subject: [PATCH] add `useReflex` js function to "support" the composable pattern This can be useful if you include a handful of mixins in your `connect()` function (e.g. some mixins from `stimulus-use`) and want them to all look similar. `StimulusReflex.register(this)` would stick out in that matter. Usage: import { Controller } from 'stimulus' import { useReflex } from 'stimulus_reflex' import { useResize, useIdle } from 'stimulus-use' export default class extends Controller { connect() { useReflex(this) useResize(this) useIdle(this) // ... } // ... } Reference: * https://github.com/stimulus-use/stimulus-use * https://www.betterstimulus.com/architecture/mixins.html --- javascript/stimulus_reflex.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/javascript/stimulus_reflex.js b/javascript/stimulus_reflex.js index 8db2d82b..bbc12802 100644 --- a/javascript/stimulus_reflex.js +++ b/javascript/stimulus_reflex.js @@ -224,6 +224,10 @@ const register = (controller, options = {}) => { }) } +const useReflex = (controller, options = {}) => { + register(controller, options) +} + document.addEventListener('stimulus-reflex:server-message', serverMessage) document.addEventListener('cable-ready:before-inner-html', beforeDOMUpdate) document.addEventListener('cable-ready:before-morph', beforeDOMUpdate) @@ -234,6 +238,7 @@ window.addEventListener('load', setupDeclarativeReflexes) export default { initialize, register, + useReflex, get debug () { return Debug.value },