javascript - Is it possible to disconnect all event handlers in Dojo? - Stack Overflow

Some code I am working with replaces some HTML elements that have Dojo event listeners with new HTML in

Some code I am working with replaces some HTML elements that have Dojo event listeners with new HTML ing from an AJAX call (using .innerHTML=). I have read that event listeners should be disconnected using the dojo.disconnect(handle) method before they are replaced to prevent memory leaks.

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

Some code I am working with replaces some HTML elements that have Dojo event listeners with new HTML ing from an AJAX call (using .innerHTML=). I have read that event listeners should be disconnected using the dojo.disconnect(handle) method before they are replaced to prevent memory leaks.

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

Share Improve this question asked May 15, 2009 at 20:36 AndreiMAndreiM 4,5984 gold badges38 silver badges50 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Actually if you are using widgets they normally should disconnect stuff in tehir destroy() method. If you are handling the nodes yourself, I see two ways you can go.

1) Manage all connects manually, means storing them somewhere. 2) Probably the safer one: store all the connect handlers in the node they connect to, like so:

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

And later you can simply disconnect them all using

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

Actually, this may work well, but there might be a more efficient way to get all connects by nodes. I just didnt find it. hth

Following the answer of Wolfram Kriesing this can 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 dojo code can be very very very ugly for multiple reasons, so maybe you want to create your own namespace.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745422789a4627040.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信