bootstrap.native: Wrong behaviour of modal.dismiss in bootstrap.native.js
Wrong condition for removing event listeners exists in Modal.dismiss method.
if (!/\bin/.test(this.modal.className)) {
this.modal.addEventListener('click', dismissHandler, false);
} else {
this.modal.removeEventListener('click', dismissHandler, false);
}
After closing dialog ‘in’ class is removed before the code above is executed, thus leading to behaviour that every close event new click listeners are attached.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 21 (10 by maintainers)
Commits related to this issue
- Major release with important changes: This is a major release that could break backward compatibility (especially for those who rely heavily on JavaScript) as we have renamed some methods, complete... — committed to thednp/bootstrap.native by thednp 7 years ago
- Major release with important changes: This is a major release that could break backward compatibility (especially for those who rely heavily on JavaScript) as we have renamed some methods, complete... — committed to thednp/bootstrap.native by thednp 7 years ago
Let’s go!
First changes includes adding a special toggled subscription routines for DOM events. The main purpose of it to generate a toggled subscribe/unsubscribe methods. First call of getSubscriptionToggler(…) returns a toggler, which when called subscribes/unsubscribes from listeners in a chainable manner;
Next, I’ve removed handlers generation from keydown and dismiss methods, and place them in common space of instance. Right after:
goes our new handlers:
And after that we declare a scoped variables keyListenerToggler and clickListenerToggler:
This will hold our togglers inside modal instance object for future use.
And we are almost finished. The last step is to refactor this.keydown and this.dismiss. It’s very simple, just use out listener togglers appropriate:
A couple other code problems in Modal:
if (!/\bin/.test(this.modal.className)) {I think you meant to put the “word boundary” on both sides of
in:if (!/\bin\b/.test(this.modal.className)) {clearTimeout(currentOpen.getAttribute('data-timer'));inthis.openlooks for an attributedata-timerthat is never set in the code. So this line of code does nothing useful.And two problems with
removeClass()itself in Utils:el.className.replace(c,'')will remove the first occurrence, e.g., of ‘in’, which, if any other class included the substring ‘in’ would lead to unintended consequences. For example, a class named ‘raining’ would become ‘raing’. So you probably need a regexp there, too. Such as:el.className.replace(new RegExp('\\b'+c+'\\b'),'')replace(/^\s+|\s+$/g,'')only trims whitespace from the beginning or end ofclassName, not the middle, whereccould have been.