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 badges2 Answers
Reset to default 5Actually 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条)