-
-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't choose first controller if no matching StimulusReflex-enabled controller was found #670
Don't choose first controller if no matching StimulusReflex-enabled controller was found #670
Conversation
✅ Deploy Preview for stimulusreflex ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
@marcoroth Shouldn't it be expanding into |
@Laykou it could, but I feel like it's weird if it "leaks" the lifecycle callbacks into a controller which potentially is not related to the reflex itself. But maybe it should? The issue could also be resolved if we do this instead, yeah: <a
data-controller="
some-sr-controller
"
data-reflex="click->Example#doSomething"
></a> to this <a
data-controller="
some-sr-controller
"
data-reflex="click->Example#doSomething"
+ data-action="click->some-sr-controller#__perform"
></a> |
How would this work then? https://docs.stimulusreflex.com/guide/lifecycle.html#callback-methods having <div data-controller="example">
<a href="#" data-reflex="Example#masticate">Eat</a>
</div> I believe it must expand to <div data-controller="example">
<a href="#" data-reflex="Example#masticate" data-action="click->example#__perform">Eat</a>
</div> in order to calll callbacks in import ApplicationController from './application_controller.js'
export default class extends ApplicationController {
beforeReflex(anchorElement) {
const { reflex } = anchorElement.dataset
if (reflex.match(/masticate$/)) anchorElement.innerText = 'Eating...'
if (reflex.match(/defecate$/)) anchorElement.innerText = 'Pooping...'
}
} Otherwise how would this |
Yeah, this is currently how it works. It will also look on any ancestor to find a matching StimulusReflex-enabled controller. <div data-controller="example">
<a href="#" data-reflex="click->Example#masticate" data-action="click->example#__perform">Eat</a>
</div> Only if the reflex specified in So this: <div data-controller="something-else">
<a href="#" data-reflex="click->Example#masticate">Eat</a>
</div> would expand into: <div data-controller="something-else">
<a href="#" data-reflex="click->Example#masticate" data-controller="stimulus-reflex" data-action="click->stimulus-reflex#__perform">Eat</a>
</div> |
Just for the record - this PR also solved a case in which i had a stimulus_reflex button with plain stimulus controller attached to it and thanks to that fact - added |
@marcoroth May I get back to this - is it possible to override the default I'd like to setup som default Is it possible to:
|
Type of PR
Bug Fix
Description
This pull request solves an edge-case for scenarios when StimulusReflex tries to detect the right controller to use for performing the StimulusReflex
__perform
controller action for executing reflexes.For example, if an element had a
data-controller
and adata-reflex
attribute, and the controller indata-controller
was a StimulusReflex-enabled controller, but didn't match the Reflex specified in thedata-reflex
there were some cases where it wouldn't add the defaultstimulus-reflex
controller to the element, but would still reference thestimulus-reflex
controller in thedata-action
attribute. This basically lead to a no-op for the reflex action.Like:
was expanded to this in some cases:
<a data-controller=" some-sr-controller " data-reflex="click->Example#doSomething" + data-action="click->stimulus-reflex#__perform" ></a>
Whereas it should have been:
With this, the
data-action
is not referencing an undefined Stimulus controller anymore and the reflex should execute again.Why should this be added
For consitancy reasons, so that the declarative reflex syntax still continues to make sense and work as expected.
Maybe this is even fixing @Laykou's issue in #659 which I couldn't reproduce.
Thanks to @foliosus for helping to debug this!
Checklist