Is it possible to disable all event handlers in Dojo?

Some code I'm working with replaces some HTML elements that have Dojo event listeners with new HTML coming from the AJAX call (using .innerHTML =). I read that event listeners should be disconnected using the dojo.disconnect (handle) method prior to replacing them to prevent memory leaks.

Is it possible to output all the handles associated with a specific element so that I can pass each one to .disconnect (handle), or is it up to me to maintain this list in my code?

0


a source to share


2 answers


In fact, if you are using widgets, they usually have to disable stuff in the tehir destroy () method. If you handle the nodes yourself, I see two ways you can go.

1) Managing all connections manually means storing them somewhere. 2) Probably safer: keep all connection handlers in the node they connect to, e.g .:

node._connectHandlers = [];
node._connectHandlers.push(dojo.connect(node, "onclick", ...));

      



And later on you can just disable them all using

dojo.query("*", nodeContainingConnects).forEach(function(node){
    if (typeof node._connectHandlers!="undefined"){
        dojo.forEach(node._connectHandlers, "dojo.disconnect(item)");
    }
});

      

This might actually work well, but there might be a more efficient way to get all connections across nodes. I just didn't find it. HTH

+5


a source


Following Wolfram Kriesing's answer, this could be "improved":

dojo._connect_tmp = dojo.connect;
dojo.connect = function (obj, event, context, method, dontFix) {
    if(obj._connectHandlers == undefined){ obj._connectHandlers = [];}
    var handler = dojo._connect_tmp (obj, event, context, method, dontFix);
    obj._connectHandlers.push(handler);
    return handler;
};

dojo.iwanttobefree = function (obj) {
   if(obj._connectHandlers == undefined) {
   } else {
      dojo.forEach(obj._connectHandlers, "dojo.disconnect(item)");  
   }
};

      

Then you can do this:



dojo.connect(myObj, 'onfocus', function(){alert('weee')});
dojo.iwanttobefree(myObj);

      

Replacing the dojo code can be very very ugly for several reasons, so you might want to create your own namespace.

+1


a source







All Articles