Skip to main content

Bug Tracker

Side navigation

Ticket #3462: event_namespaces.patch
File event_namespaces.patch, 4.2 KB (added by morgan, October 08, 2008 02:10PM UTC)

Patch to add better event namespace support

Index: jquery/src/event.js
===================================================================
--- jquery/src/event.js	(revision 5859)
+++ jquery/src/event.js	(working copy)
@@ -54,14 +54,15 @@
 			// Namespaced event handlers
 			var parts = type.split(".");
 			type = parts[0];
-			handler.type = parts[1];
+			handler.type = namespace = parts[1] || "*";
 
 			// Get the current list of functions bound to this event
 			var handlers = events[type];
 
 			// Init the event handler queue
 			if (!handlers) {
-				handlers = events[type] = {};
+                                events[type] = {};
+				handlers = events[type][namespace] = {};
 
 				// Check for a special event handler
 				// Only use addEventListener/attachEvent if the special
@@ -73,10 +74,16 @@
 					else if (elem.attachEvent)
 						elem.attachEvent("on" + type, handle);
 				}
+                                handlers[handler.guid] = handler;
 			}
+                        else
+                        {
+                                // Add the function to the element's handler list
+                                if(!handlers[namespace])
+                                        handlers[namespace] = {};
+                                handlers[namespace][handler.guid] = handler;
+                        }
 
-			// Add the function to the element's handler list
-			handlers[handler.guid] = handler;
 
 			// Keep track of which events have been used, for global triggering
 			jQuery.event.global[type] = true;
@@ -115,21 +122,22 @@
 					// Namespaced event handlers
 					var parts = type.split(".");
 					type = parts[0];
+                                        namespace = parts[1] || "*";
 
-					if ( events[type] ) {
+					if ( events[type] && events[type][namespace] ) {
 						// remove the given handler for the given type
 						if ( handler )
-							delete events[type][handler.guid];
+							delete events[type][namespace][handler.guid];
 
 						// remove all handlers for the given type
 						else
-							for ( handler in events[type] )
+							for ( handler in events[type][namespace] )
 								// Handle the removal of namespaced events
-								if ( !parts[1] || events[type][handler].type == parts[1] )
-									delete events[type][handler];
+								if ( !parts[1] || events[type][namespace][handler].type == parts[1] )
+									delete events[type][namespace][handler];
 
 						// remove generic event handler if no more handlers exist
-						for ( ret in events[type] ) break;
+						for ( ret in events[type][namespace] ) break;
 						if ( !ret ) {
 							if ( !jQuery.event.special[type] || jQuery.event.special[type].teardown.call(elem) === false ) {
 								if (elem.removeEventListener)
@@ -138,7 +146,10 @@
 									elem.detachEvent("on" + type, jQuery.data(elem, "handle"));
 							}
 							ret = null;
-							delete events[type];
+							delete events[type][namespace];
+                                                        for ( ret in events[type] ) break;
+                                                        if( !ret )
+        							delete events[type];
 						}
 					}
 				});
@@ -246,17 +257,27 @@
 		// Namespaced event handlers
 		namespace = event.type.split(".");
 		event.type = namespace[0];
-		namespace = namespace[1];
+		namespace = namespace[1] || "*";
 		// Cache this now, all = true means, any handler
-		all = !namespace && !event.exclusive;
-
+		all = (!namespace || namespace == "*") && !event.exclusive;
 		handlers = ( jQuery.data(this, "events") || {} )[event.type];
 
+                if(handlers && all)
+                {
+                        var h = {};
+                        for( var i in handlers )
+                                for( k in handlers[i] )
+                                        h[k] = handlers[i][k];
+                        handlers = h;
+                }
+                else if(handlers && !all)
+                        handlers = handlers[namespace];
+
 		for ( var j in handlers ) {
 			var handler = handlers[j];
 
 			// Filter the functions by class
-			if ( all || handler.type == namespace ) {
+			if ( all || handler.type == namespace || namespace == "*" ) {
 				// Pass in a reference to the handler function itself
 				// So that we can later remove it
 				event.handler = handler;

Download in other formats:

Original Format