Skip to content
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

Initialize firing twice #215

Closed
leastbad opened this issue Dec 30, 2018 · 6 comments
Closed

Initialize firing twice #215

leastbad opened this issue Dec 30, 2018 · 6 comments

Comments

@leastbad
Copy link
Contributor

I am seeing Stimulus fire the initialize event twice when I include two instances of a controller in a document. I am under the strong impression that I should see two connect events but only one initialize event per page load.

This happens even without Turbolinks in the picture:

https://codepen.io/pjforde1978/pen/aPVzbR?editors=1011

Apologies if I'm doing it wrong. It seems unlikely that nobody else would have noticed this, and yet I am unable to come up with a simpler example.

@javan
Copy link
Contributor

javan commented Dec 30, 2018

That's expected. Each controller instance initialize()s once and only once when its element first connect()s, but may connect() and disconnect() multiple times if the element is reconnected to the DOM. See https://stimulusjs.org/reference/lifecycle-callbacks.

Here's a quick demo to help illustrate: https://codepen.io/javan/pen/dwZPmo?editors=1011

@javan javan closed this as completed Dec 30, 2018
@leastbad
Copy link
Contributor Author

Thank you so much for the insanely fast reply.

For what it's worth, I couldn't find anything in the docs or closed issues that explains this. It might be a good candidate for inclusion in the next round of documentation errata.

@javan
Copy link
Contributor

javan commented Dec 30, 2018

For what it's worth, I couldn't find anything in the docs or closed issues that explains this.

Did you see https://stimulusjs.org/handbook/managing-state#lifecycle-callbacks-explained and / or https://stimulusjs.org/reference/lifecycle-callbacks?

@leastbad
Copy link
Contributor Author

Yes, and I just read them again, now, to be sure.

There’s nothing that explicitly spells out that initialize fires once per instantiation. The addition of a short note might save future noobs the confusion I experienced this morning. 🤓

@javan
Copy link
Contributor

javan commented Dec 30, 2018

There’s nothing that explicitly spells out that initialize fires once per instantiation

screen shot 2018-12-30 at 8 31 25 am

@leastbad
Copy link
Contributor Author

I understand what we see differently. What I’m trying to explain is that to a newcomer, your use of the word Once instead of the term “Once per instance/occurrence” is just ambiguous enough to be confusing.

The one thing I have is the perspective of an experienced developer doing work with the library for the first time. If you don’t think it’s worth explaining further, no sweat. I really appreciate your explanation regardless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants