Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Pass event data as arguments to the handler. #183

Closed
wants to merge 1 commit into from

2 participants

@johnboxall

Mimic jQuery's custom event triggering syntax - http://api.jquery.com/trigger/

Allows you to write:

$('#foo').bind('custom', function(event, param1, param2) {
  alert(param1 + "\n" + param2);
});
$('#foo').trigger('custom', ['Custom', 'Event']);

Rather than:

$('#foo').bind('custom', function(event, params) {
  alert(params[0] + "\n" + params[1]);
});
$('#foo').trigger('custom', ['Custom', 'Event']);
@mislav mislav closed this pull request from a commit
@johnboxall johnboxall event handlers now receive custom data in multiple arguments
For jQuery compatibility.

closes #183
11de4dc
@mislav mislav closed this in 11de4dc
@stereobooster stereobooster referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@stereobooster stereobooster referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 30, 2011
  1. @johnboxall
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 1 deletion.
  1. +3 −1 src/event.js
  2. +25 −0 test/zepto.html
View
4 src/event.js
@@ -26,7 +26,9 @@
var id = zid(element), set = (handlers[id] || (handlers[id] = []));
events.split(/\s/).forEach(function(event){
var callback = delegate || fn;
- var proxyfn = function(event) { return callback.call(element, event, event.data) };
+ var proxyfn = function(event) {
+ return callback.apply(element, [event].concat(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, proxyfn, false);
View
25 test/zepto.html
@@ -1293,6 +1293,31 @@
};
$('#some_form').submit();
t.assert(formSubmitted);
+ },
+
+ testCustomEvents: function (t) {
+ var $body = $(document.body);
+
+ $body.bind('custom', function(evt, a, b) {
+ t.assertEqual(a, 1);
+ t.assertEqual(b, 2);
+ $body.unbind();
+ })
+ $body.trigger('custom', [1, 2]);
+
+ $body.bind('custom', function(evt, a) {
+ t.assertEqual(a, 1);
+ $body.unbind();
+ })
+ $body.trigger('custom', 1);
+
+ var eventData = {z: 1};
+ $body.bind('custom', function(evt, a) {
+ t.assertEqual(a, eventData);
+ $body.unbind();
+ })
+ $body.trigger('custom', eventData);
+
}
});
</script>
Something went wrong with that request. Please try again.