-
Notifications
You must be signed in to change notification settings - Fork 19
Feature/811 once and context #812
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Late review, sorry.
TL;DR: I think this is good but needs a guard clause to complete #811; I didn't have a chance to test it.
Using .once() will only return DOM elements once, but I think the behavior will still run multiple times.
The second part we usually add is one or more guard clauses (see suggestion). This makes sure that, on subsequent runs, the behavior doesn't try to do anything and exits immediately.
For example, if this behavior ran twice, the first go would mark the menu and toggle with a data-once attribute and continue through the code. But on the second iteration, I think the two variables will be undefined, and line 17 will try to add an event listener to an undefined element.
| ); | ||
| const subsitesMenu = document.querySelector('.subsite-extra-menu'); | ||
| const [subsitesMenuToggle] = once('allSubsitesMenuToggles', '.subsite-extra__header-toggle-button', context); | ||
| const [subsitesMenu] = once('allSubsitesMenuToggles', '.subsite-extra-menu', context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const [subsitesMenu] = once('allSubsitesMenuToggles', '.subsite-extra-menu', context); | |
| const [subsitesMenu] = once('allSubsitesMenuToggles', '.subsite-extra-menu', context); | |
| if (!subsitesMenuToggle || !subsitesMenu) { | |
| return; | |
| } |
This is untested, but it's the usual defensive companion to .once().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ctorgalson, I've added in the extra defensive clause now. Let me know if you think we're missing any thinbg.
Closes #811