Skip to content

Commit

Permalink
improve delegation for focus/blur and enable unbind
Browse files Browse the repository at this point in the history
References madrobby#597, references madrobby#552
  • Loading branch information
mislav committed Sep 27, 2012
1 parent bbb5ca1 commit 74bd6f5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@
else events.split(/\s/).forEach(function(type){ iterator(type, fn) })
}

function eventCapture(handler, captureSetting) {
return handler.del &&
(handler.e == 'focus' || handler.e == 'blur') ||
!!captureSetting
}

function add(element, events, fn, selector, getDelegate, capture){
capture = !!capture
var id = zid(element), set = (handlers[id] || (handlers[id] = []))
eachEvent(events, fn, function(event, fn){
var delegate = getDelegate && getDelegate(fn, event),
Expand All @@ -47,15 +52,15 @@
}
var handler = $.extend(parse(event), {fn: fn, proxy: proxyfn, sel: selector, del: delegate, i: set.length})
set.push(handler)
element.addEventListener(handler.e, proxyfn, capture)
element.addEventListener(handler.e, proxyfn, eventCapture(handler, capture))
})
}
function remove(element, events, fn, selector){
function remove(element, events, fn, selector, capture){
var id = zid(element)
eachEvent(events || '', fn, function(event, fn){
findHandlers(element, event, fn, selector).forEach(function(handler){
delete handlers[id][handler.i]
element.removeEventListener(handler.e, handler.proxy, false)
element.removeEventListener(handler.e, handler.proxy, eventCapture(handler, capture))
})
})
}
Expand Down Expand Up @@ -128,8 +133,6 @@
}

$.fn.delegate = function(selector, event, callback){
var capture = /^(?:blur|focus)(?:\..*)?$/.test(event)

return this.each(function(i, element){
add(element, event, callback, selector, function(fn){
return function(e){
Expand All @@ -139,7 +142,7 @@
return fn.apply(match, [evt].concat([].slice.call(arguments, 1)))
}
}
}, capture)
})
})
}
$.fn.undelegate = function(selector, event, callback){
Expand Down
24 changes: 24 additions & 0 deletions test/zepto.html
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,30 @@ <h1>Zepto DOM unit tests</h1>
t.assertEqual(4, counter)
},

testUndelegateNamespacedBlurFocus: function(t) {
var el, counter = 0

el = $('#delegate_blur_test')

el.delegate('input', 'blur.test', function(){ counter++ })
el.find('input').focus().blur()
t.assertEqual(1, counter, 'expected handler to be triggered')

el.undelegate('input', '.test')
el.find('input').focus().blur()
t.assertEqual(1, counter, 'expected handler to unbind')

el = $('#delegate_focus_test')

el.delegate('input', 'focus.test', function(){ counter++ })
el.find('input').focus().blur()
t.assertEqual(2, counter, 'expected handler to be triggered')

el.undelegate('input', '.test')
el.find('input').focus().blur()
t.assertEqual(2, counter, 'expected handler to unbind')
},

testDelegateReturnFalse: function(t){
$(document.body).delegate('#some_element', 'click', function(){ return false })

Expand Down

0 comments on commit 74bd6f5

Please sign in to comment.