views:

606

answers:

2

How do you remove a dojo connected event if you do not have the "handle" that was returned during the dojo.connect?

My example involves dynamically assigning a set of events to a set of objects. (for simplicity, the events are onclick and ondblclick, and the objects are rows within a table)

So, during page setup events are connected to each row (onclick, ondblclick). Now, depending on user desires/actions, a need for removal of one event from one row is required. But the original handle is no longer available. Because of this the following will not work: dojo.disconnect(row, "onclick", ??*). How do I get around this without hacking the original row structure?

Any help is of course greatly appreciated.

+1  A: 

What I usually do is save the handles when I create them so I can disconnect them later. Something like:

 var connects = {};

 // then later on
 var node = dojo.create(....); // or someting else that gives you a node
 dojo.forEach( ['click','ondblclick' ], function( evt, idx ) {
   if (!connects[node.id]) {
     connects[node.id] = [];
   }
   connects[ node.id ][idx] = dojo.connect( node, evt, function(evt) { .... });
 });

Then later, on you can disconnect like:

 dojo.forEach( connects[node.id], function( handle ) {
   dojo.disconnect( handle );
 });

There's a similar code sample for this on dojocampus

seth
connects[node.id] is undefined => you have to assign {} or [] to it first.
Eugene Lazutkin
thanks. updated answer.
seth
+3  A: 
Eugene Lazutkin