Skip to content
Browse files

add el arg to .unbind()

  • Loading branch information...
1 parent f62a854 commit 20550b5545c06b2a0c007f2f8e43774d64455afa @tj tj committed
Showing with 19 additions and 20 deletions.
  1. +1 −1 Readme.md
  2. +5 −12 index.js
  3. +13 −7 test/index.html
View
2 Readme.md
@@ -29,7 +29,7 @@ var fn = delegate.bind(ul, 'li a', 'click', function(e){
Bind and return a callback which may be passed to `.unbind()`.
-### .unbind(type, callback, [capture])
+### .unbind(el, type, callback, [capture])
Unbind.
View
17 index.js
@@ -6,12 +6,6 @@
var matches = require('matches-selector');
/**
- * Contained by flag.
- */
-
-var containedBy = 16;
-
-/**
* Delegate event `type` to `selector`
* and invoke `fn(e)`. A callback function
* is returned which may be passed to `.unbind()`.
@@ -26,12 +20,10 @@ var containedBy = 16;
*/
exports.bind = function(el, selector, type, fn, capture){
- document.addEventListener(type, callback, capture);
+ el.addEventListener(type, callback, capture);
function callback(e) {
- if (el.compareDocumentPosition(e.target) & containedBy) {
- if (matches(e.target, selector)) fn(e);
- }
+ if (matches(e.target, selector)) fn(e);
}
return callback;
@@ -40,12 +32,13 @@ exports.bind = function(el, selector, type, fn, capture){
/**
* Unbind event `type`'s callback `fn`.
*
+ * @param {Element} el
* @param {String} type
* @param {Function} fn
* @param {Boolean} capture
* @api public
*/
-exports.unbind = function(type, fn, capture){
- document.removeEventListener(type, fn, capture);
+exports.unbind = function(el, type, fn, capture){
+ el.removeEventListener(type, fn, capture);
};
View
20 test/index.html
@@ -10,10 +10,8 @@
<li><a href="#">Three</a></li>
</ul>
- <ul>
+ <ul id="list-two">
<li><a href="#">One</a></li>
- <li><a href="#">Two</a></li>
- <li><a href="#">Three</a></li>
</ul>
<button>Button</button>
@@ -22,14 +20,22 @@
<script>
var delegate = require('delegate');
- var ul = document.querySelector('ul');
+ var uls = document.querySelectorAll('ul');
var n = 0;
- var fn = delegate.bind(ul, 'li a', 'click', function(e){
+ var fn = delegate.bind(uls[0], 'li a', 'click', function(e){
+ console.log(e.target);
+ if (++n >= 3) {
+ console.log('unbind');
+ delegate.unbind(uls[0], 'click', fn, false);
+ }
+ }, false);
+
+ var fn2 = delegate.bind(uls[1], 'li a', 'click', function(e){
console.log(e.target);
- if (++n == 3) {
+ if (++n >= 8) {
console.log('unbind');
- delegate.unbind('click', fn, false);
+ delegate.unbind(uls[1], 'click', fn2, false);
}
}, false);
</script>

0 comments on commit 20550b5

Please sign in to comment.
Something went wrong with that request. Please try again.