Wednesday, August 13, 2008

Dojo: Event handling and the Firebug Console

I've spent some time working with dojo and looking at registering event listeners using dojo.connect. I then write the type of event out to firebug's console.

This example has been tested with the latest version of dojo at the time of writing (1.1.1) and Firebug 1.2.0b7
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Testing the dojo event model with Firebug</title>

<style type="text/css">
body {font-family:Verdana, Arial, Helvetica, sans-serif; font-size:80%;}
#t1 {display:block; width:100px; height:100px; border:1px dotted red;}
#t2 {display:block; width:100px; height:100px; border:1px dotted blue;}
</style>

<script type="text/javascript" src="dojo/dojo/dojo.js"></script>
<script type="text/javascript">

window.onload = function () {
/* register the divs with dojo */
myLink = dojo.byId("t1");
myLink2 = dojo.byId("t2");

// create an array of all listeners and register them
myLinkConnections = [];
myLinkConnections[0] = dojo.connect(myLink, "onmouseover", myHandler);
myLinkConnections[1] = dojo.connect(myLink, "onmouseout", myHandler);
myLinkConnections[2] = dojo.connect(myLink, "onclick", myHandler);

myLinkConnections2 = [];
myLinkConnections2[0] = dojo.connect(myLink2, "onmouseover", myHandler2);
myLinkConnections2[1] = dojo.connect(myLink2, "onmouseout", myHandler2);
myLinkConnections2[2] = dojo.connect(myLink2, "onclick", myHandler2);

}

function myHandler(evt) {
console.log('[myHandler]', evt);

}

function myHandler2(evt) {
console.info('[myHandler2]', evt);
}

</script>


</head>

<body>
<div id="t1">first action</div>
<div id="t2">second action</div>
</body>
</html>

0 comments:

Blog Archive

About Me