Permalink
Browse files

Added support for `blur` & `focus` in `delegate()`

Manipulated `blur` & `focus` into `focusout` & `focusin` so that the events bubble and can be caught.
  • Loading branch information...
joelambert committed Feb 21, 2012
1 parent 6f80dae commit 87388928cdbba257282c711cbab81850f03ec522
Showing with 27 additions and 0 deletions.
  1. +1 −0 src/event.js
  2. +26 −0 test/zepto.html
View
@@ -115,6 +115,7 @@
}
$.fn.delegate = function(selector, event, callback){
+ event = event == 'blur' ? 'focusout' : event == 'focus' ? 'focusin' : event;
return this.each(function(i, element){
add(element, event, callback, selector, function(fn){
return function(e){
View
@@ -164,6 +164,9 @@ <h1>Zepto DOM unit tests</h1>
<div id="delegate_test"><span class="first-level"><span class="second-level">hi</span></span></div>
<div id="undelegate_test"><span class="first-level"><span class="second-level">hi</span></span></div>
+ <div id="delegate_blur_test"><input type="text"></div>
+ <div id="delegate_focus_test"><input type="text"></div>
+
<div id="another_element"></div>
<div id="namespace_test"></div>
@@ -1838,6 +1841,29 @@ <h1>Zepto DOM unit tests</h1>
t.assertEqual(3, pcounter);
},
+ testDelegateBlurFocus: function(t) {
+ var counter = 0;
+ $('#delegate_blur_test').delegate('input', 'blur', function(){ counter++; });
+
+ $('#delegate_blur_test').find('input').focus();
+ $('#delegate_blur_test').find('input').blur();
+ t.assertEqual(1, counter);
+
+ $('#delegate_blur_test').find('input').focus();
+ $('#delegate_blur_test').find('input').blur();
+ t.assertEqual(2, counter);
+
+ $('#delegate_focus_test').delegate('input', 'focus', function(){ counter++; });
+
+ $('#delegate_focus_test').find('input').focus();
+ $('#delegate_focus_test').find('input').blur();
+ t.assertEqual(3, counter);
+
+ $('#delegate_focus_test').find('input').focus();
+ $('#delegate_focus_test').find('input').blur();
+ t.assertEqual(4, counter);
+ },
+
testDelegateReturnFalse: function(t){
$(document.body).delegate('#some_element', 'click', function(){ return false });

0 comments on commit 8738892

Please sign in to comment.