Here's two ideas for extensions to the jQuery event system...
Namespace Storage
Currently, anything after the first '.' is stripped from the type property on the event object so handlers have no way of knowing what the namespaced event was called...
$('.foo').bind('name.space',function(event) {
alert(event.type); // 'name'
}):
$('.foo').trigger('name.space');
If the full event namespace (and it's compoent parts which are also readily available) were stored on the event object, we could do things like this...
$('.foo').bind('ui',function(event) {
switch (event.namespace) {
case 'ui.refresh':
// do stuff
break;
case 'ui.delete':
// do stuff
break;
case 'ui.update':
// do stuff
break;
}
}):
// elsewhere in the ui control...
$('.foo').trigger('ui.update');
The event.namespace property would make it really easy for a single handler to manage multiple events of the same type without resorting to passing additional data objects around.
Providing this property and it's twin, the array of namespace sections, is trivial. Here's what the jQuery 1.2.3 handle() method looks like around line 2059:
// Namespaced event handlers var parts = event.type.split("."); event.type = parts[0];
A couple of tweaks and we get two very useful new properties on the event object:
// Namespaced event handlers var parts = event.parts = (event.namespace = event.type).split("."); event.type = parts[0];
The MSIE property clean up later in the method would need to remove the two items, but it's certainly very little work to get some really useful properties on to the event object.
Go vote by commenting on the feature request ticket ![]()
Event Config
I'd dearly love to be able to do this:
$('.foo').bind('keyboard(ctrl+c).mystuff',handler);
The brackets and contents are optional, but if defined they'll provide config data for the event. Again, this is much cleaner than passing data objects around.
How this would be implemented, I have no idea! It's really config for the centralised event system, not the external handlers that get triggered by the event.


Comments (1)
Sep 07, 2009
Mark Gibson says:
The namespaces are attached to the handler function in the type property. This b...The namespaces are attached to the handler function in the type property. This bit of code (paste it into firebug) demonstrates:
$(document).bind('click.myname.yourname', function() { console.log(arguments.callee.type); });As for binding keyboard shortcuts... [jQuery:Keys].