JavaScript event Binding (dojo.connect, dojo.disconnect , jquery's bind/unbind )
When you use Web 2.0 AJAX calls, there are lot of things which needs to be taken into consideration to avoid memory leaks problems. One of the major problem is EventListener references are left when you update DOM with fresh HTML contents. With advance Web 2.0 application, when you work in complex mash-up based application it is impossible to track down all the events associated with your DOM Structure. Currently there is no standard method which can return list of EventHandler associated with a DOM Element.
Your life can be easy if you are using popular open source JS Framework like jQuery, Dojo etc. For unregistering events jQuery provides a utility function unbind, which can remove all the events associated with that element... but hold on.. it will remove only those events which were registered using jQuery's bind utility.
It means jQuery internally manages *some* data-structure to keep track of all event registration. When you call unbind to remove all handlers, it uses its internal data structure to remove events registered. If you have not used bind utility then those events still remains in the memory. See the sample HTML, demonstrating the same (manually written onclick is still executed even after unbinding the event).
For this matter, dojo does not maintain any such internal design pattern (Smart decision, as if you don't have handle completely; don't provide feature rather then misleading one).
There are ways you can at least keep track of events which are registered and which are unregistering themselves (as expected). You can put a handle by using JavaScript event handling. Below here code snippet shows how you can track for events which are registered using dojo.connect :
function trackDojoConnect(DOMObject,eventName,functionObject){
console.log(DOMObject);
console.log(eventName);
console.log(functionObject);
}
function trackDojoDisconnect(dojoHandlerForEventTracking){
console.log(dojoHandlerForEventTracking);
}
dojo.connect = trackDojoConnect ;
dojo.disconnect = trackDojoDisconnect ;
/*
Provided you are using
dojo.connect(DOMElement,"onclick",functionName); like dojo.connect(button2,"onclick",test1);
The above code works perfect.
*/
Probably this approach is being used by most dojo explorer plug-in to track dojo.connect and dojo.disconnet. If you want to automate unregistering of events then you can probably use above approach to register all the elements utilizing dojo.connect and then calling dojo.disconnect for all the handlers associated with that element. I am sure this idea would work perfect.
For now, as I told, there are no STANDARD method/mechanism to get all registered event handler from a DOM Element.
Hope this helps-
















