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.
The answer to your question is that is it impossible to disconnect event if you have lost the "handle" that was returned during the dojo.connect operation. Except if you are ready to do heavy hacking of dojo code itself.
You must store these somewhere if you wish to disconnect them later on.
Like Seth I have my patterns for removing events — I collect handles in arrays (pseudo-code):
var handles = [
dojo.connect(...),
dojo.connect(...),
dojo.connect(...)
];
If I connect dynamically I just collect them:
var handles = [];
...
handles.push(dojo.connect(...));
...
handles.push(dojo.connect(...));
If I connect several events in a regular manner I can use dojo.map()
:
var handles = dojo.map(["click", "ondblclick"], function(evt){
return dojo.connect(node, evt, ...);
});
The neat part is that later I can disconnect them all with a handy one-liner:
dojo.forEach(handles, dojo.disconnect);
In your case you can use a dictionary like shown by Seth to accomplish the same:
var handles = {};
...
handles[node.id] = dojo.map(["click", "ondblclick"], function(evt){
return dojo.connect(node, evt, ...);
});
So later one you can disconnect handlers like this:
dojo.forEach(handles[node.id], dojo.disconnect);
See if Seth' and my sketches help in your case. If not, give us more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With