Permalink
Browse files

Modified event.js to add support for custom event data. Should solve …

…issue #143
  • Loading branch information...
1 parent f6bd11f commit 532523562e7ac5e1c7a27ba8ae58859dc6fa1a50 @andregoncalves andregoncalves committed Apr 21, 2011
Showing with 39 additions and 7 deletions.
  1. +11 −7 src/event.js
  2. +28 −0 test/zepto.html
View
@@ -25,17 +25,19 @@
function add(element, events, fn, selector, delegate){
var id = zid(element), set = (handlers[id] || (handlers[id] = []));
events.split(/\s/).forEach(function(event){
- var handler = $.extend(parse(event), {fn: fn, sel: selector, del: delegate, i: set.length});
+ var callback = delegate || fn;
+ var proxyfn = function(event) { return callback(event, event.data) };
+ var handler = $.extend(parse(event), {fn: fn, proxy: proxyfn, sel: selector, del: delegate, i: set.length});
set.push(handler);
- element.addEventListener(handler.e, delegate || fn, false);
+ element.addEventListener(handler.e, proxyfn, false);
});
}
function remove(element, events, fn, selector){
var id = zid(element);
(events || '').split(/\s/).forEach(function(event){
findHandlers(element, event, fn, selector).forEach(function(handler){
delete handlers[id][handler.i];
- element.removeEventListener(handler.e, handler.del || handler.fn, false);
+ element.removeEventListener(handler.e, handler.proxy, false);
});
});
}
@@ -80,13 +82,13 @@
$.fn.delegate = function(selector, event, callback){
return this.each(function(i, element){
- add(element, event, callback, selector, function(e){
+ add(element, event, callback, selector, function(e, data){
var target = e.target, nodes = $$(element, selector);
while (target && nodes.indexOf(target) < 0) target = target.parentNode;
if (target && !(target === element) && !(target === document)) {
callback.call(target, $.extend(createProxy(e), {
currentTarget: target, liveFired: element
- }));
+ }), data);
}
});
});
@@ -106,10 +108,12 @@
return this;
};
- $.fn.trigger = function(event){
+ $.fn.trigger = function(event, data){
return this.each(function(){
var e = document.createEvent('Events');
- this.dispatchEvent(e, e.initEvent(event, true, true));
+ e.initEvent(event, true, true)
+ e.data = data;
+ this.dispatchEvent(e);
});
};
})(Zepto);
View
@@ -620,6 +620,15 @@
t.assertEqual(3, counter); // 1 = body click, 2 = element click, 3 = element mousedown
},
+ testBindWithCustomData: function(t) {
+ var counter = 0;
+ var handler = function(ev,customData) { counter = customData.counter };
+
+ $('#some_element').bind('custom', handler);
+ $('#some_element').trigger('custom', { counter: 10 });
+ t.assertEqual(10, counter);
+ },
+
testUnbind: function(t){
var counter = 0, el = $('#another_element').get(0);
var handler = function(){ counter++ };
@@ -693,6 +702,25 @@
t.assertEqual(3, pcounter);
},
+ testDelegateWithCustomData: function(t) {
+ var counter = 0, pcounter = 0, content = '';
+ $(document.body).delegate('#some_element', 'custom', function(ev,customData) { counter += customData.counter });
+ $('p').delegate('span.yay', 'custom', function(ev,customData) { counter += customData.counter });
+ $(document.body).delegate('p', 'custom', function(ev,customData) { pcounter += customData.counter });
+
+ $('#some_element').trigger('custom', { counter: 1 });
+ t.assertEqual(1, counter);
+
+ $('span.yay').trigger('custom', { counter: 1 });
+ t.assertEqual(2, counter);
+
+ $('span.nay').trigger('custom', { counter: 1 });
+ t.assertEqual(2, counter);
+
+ $('p').first().trigger('custom', { counter: 1 });
+ t.assertEqual(3, pcounter);
+ },
+
testDelegateEventProxy: function(t){
$('div#delegate_test').delegate('span.second-level', 'click', function(e){
t.assertEqual($('span.second-level').get(0), this);

0 comments on commit 5325235

Please sign in to comment.