Event dispatching with jQuery

Gabor and I did some thinking and coding. This is an other way to handle your events

The script

beneath you will find a plugin for jQuery

/*
* Distill
* a extremely fast event handling solution
* written by:
* Gabor de Mooij
* Robert Cabri
*/
$.distill = function(evType,obj)
{
	
	var dispatcher = function(ev,et,handler)
	{
		try{
			var classes = [];
			var clsStr = $(ev.target || ev.srcElement).attr("class");
			if (clsStr.indexOf(" ")>-1) {
				classes = clsStr.split(" ")
			} else {
				classes = [clsStr];
			}
			
			for (var j in classes)
			{
				var hname = classes[j].replace(/com_/,'');
				
				if (handler[hname])
				{
					if (handler[hname][et])
					{
						handler[hname][et](ev,et);
					}
				}
			}
		}catch(e){}
	}
	
	var evTypes;
	
	if (evType.indexOf(",") > -1)
	{
		evTypes = evType.split(",");
	}
	else
	{
		evTypes = [evType];
	}
		
	for(var j in evTypes)
	{
		
		$("body")[evTypes[j]]((function(evt){
			return function(e){
				dispatcher(e,evt,obj);
			}
		})(evTypes[j]));	
	}
}

Usage

To use this script you need the jQuery library! See a working version

<html>
	<head>
		<title>
		</title>
		<script src="jquery-1.2.1.js"></script>
		<script src="distill.js"></script>
		<script>
		
			//test
			
			//attach a keyup and a select handler to the textarea
		
			window.onload = function ()
			{
				
				$.distill("keyup,select,click",{
					myobj :{
						select:function()
						{
							alert("textarea 1 select");
						},
						keyup: function()
						{
							alert("textarea 1 keyup");
						}
					},
					myobj2: {
						click: function()
						{
							alert("textarea 2 click");
						},
						keyup: function()
						{
							alert("textarea 2 keyup");
						}
						
					}
				});
				
			}
		
		</script>
	</head>
	<body>
		<textarea class='bla com_myobj'>text 1</textarea>
		<textarea class='com_myobj2'>text 2</textarea>
	</body>
</html>