-
-
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 add data-controller
to reflex element if parent already holds instance
#636
Don't add data-controller
to reflex element if parent already holds instance
#636
Conversation
✅ Deploy Preview for stimulusreflex ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
javascript/test/reflexes.setupDeclarativeReflexesForElement.test.js
Outdated
Show resolved
Hide resolved
I want to emphasize the importance of this PR, indeed if we want to add some Stimulus controls (on I'd really like to be able to have only one controller! |
Hi, Are you planning to finalize & merge this PR? Thanks |
Hey @arambert, yeah I wanted to get this merged soon! I've been exploring some other edge cases, which I wanted to address in the pull request. But either way, I'm going to finish this one as-is! Thanks for your patience! 🙏🏼 |
… controllers connects later Also changes `scanForReflexesOnElement` to accept a controller argument, whcih can be used to pass over a not-yet connected controller to properly figure out the correct controller identifier for a StimulusReflex action
Type of PR
Bug fix
Description
There's was an edge case where we would add another
data-controller
attribute to the reflex element even if a parent element already had a matching controller instance.This markup:
would get transformed to:
Which ends up with two controller instances of
example
even if we actually just needed one.This pull request optimizes this by just conditionally adding the
example
controller to thedata-controller
attribute on the reflex element.Now with this pull request the end-result is just:
<div data-controller="example"> <button data-reflex="click->Example#increment" + data-action="click->example#__perform" >Click</button> </div>
Why should this be added
We shouldn't create more controller instances than necessary. In this case it was apparent because the
connect()
callback was fired twice. To not fire the method twice we add the condition.Checklist