Skip to content

Commit

Permalink
remove all listeners unconditionally, also fixes #221 (#220)
Browse files Browse the repository at this point in the history
* remove all listeners unconditionally

if neither event type nor callback has been given, remove all listeners regardless of capture value. This avoid iterating through the array twice for cleaning up, once for capture=true and once for capture=false.

* added comments on unbinding all listeners

* allow unbinding en masse by capture setting

added checks of boolean param in unbind function  to allow the following scenarios
1. removing all listeners: $.unbind(elem)
2. removing all listeners with capture option set to false: $.unbind(elem, undefined, false)
3. removing all listeners with capture option set to true: $.unbind(elem, undefined, true)

* added examples of unbinding by capture setting

* modified comments on Bliss Shy, bind and unbind

* corrected a typo in parentheses

should have used local editor and git.

* updated unbind examples

somehow after function overloading and minification, $.unbind(elem,'', false) and $.unbind(elem,'', true) work as expected. The type param placeholder needs to be an empty string to represent all event types. The types placeholder can not be undefined.

* fixed memory leak bug [#221]

when data arrays exist in $.listeners, always go through data arrays to clean up and remove event listeners.
  • Loading branch information
hopeful2 authored and LeaVerou committed Dec 9, 2018
1 parent 6ca1093 commit 834e6bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 7 additions & 5 deletions bliss.shy.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,11 +628,11 @@ $.Element.prototype = {
type = type[0];
}

if (type && options.callback) {
return $.original.removeEventListener.call(this, type, options.callback, options.capture);
}

//if listeners exist, always go through listeners to clean up
if (!listeners) {
if (type && options.callback) {
return $.original.removeEventListener.call(this, type, options.callback, options.capture);
}
return;
}

Expand All @@ -643,7 +643,9 @@ $.Element.prototype = {
for (var i=0, l; l=listeners[ltype][i]; i++) {
if ((!className || className === l.className)
&& (!options.callback || options.callback === l.callback)
&& (!!options.capture == !!l.capture)) {
&& (!!options.capture == !!l.capture ||
!type && !options.callback && undefined === options.capture)
) {
listeners[ltype].splice(i, 1);
$.original.removeEventListener.call(this, ltype, l.callback, l.capture);
i--;
Expand Down
11 changes: 10 additions & 1 deletion docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,9 @@ <h1 class="element array">unbind</h1>
<dd>The event type. You can include multiple event types by space-separating them.
<strong>If using Bliss Full</strong>:
You can also unbind by class (e.g. <code>click.foo</code> or even just <code>.foo</code> to unbind all events with that class).
You can also unbind all listeners on the element, by not providing <strong>any</strong> arguments.
You can also unbind all listeners on the element, by not providing <strong>any</strong> arguments. This will remove all listeners with capture option set to either true or false.
This works for Bliss Shy and anonymous functions too, if the listeners were added using Bliss bind method. Bliss (both Full and Shy) keeps track of anonymous functions so these listeners may be removed later.

</dd>

<dt class="function">callback</dt>
Expand All @@ -904,6 +906,13 @@ <h1 class="element array">unbind</h1>
// Clicking on the body now prints only bar mousedown
document.body._.unbind(); // unbind ALL events
// Clicking on the body now does nothing
$.unbind(elem)
// Removing all listeners from elem, regardless of event capture setting.
// If using Bliss Shy, $.unbind only works for the listeners added using $.bind method
$.unbind(elem, '', false)
// Removing all listeners from elem, with capture option set to false (the default in most browsers)
$.unbind(elem, '', true)
// Removing all listeners from elem, with capture option set to true explicitly
</code></pre>

<ul class="notes">
Expand Down

0 comments on commit 834e6bd

Please sign in to comment.