views:

350

answers:

2

When I .destroy() an Element object in MooTools, does .destroy() automatically internally call element.removeEvents(), or do I need to keep this in mind. (I'm removing elements from the DOM that have previously had element.addEvent() called.)

+1  A: 

.destroy() in MooTools, version 1.2.4:

destroy: function(){
    Element.empty(this);
    Element.dispose(this);
    clean(this, true);
    return null;
}

The clean(item, retain) function does .removeEvents() if the browser needs it:

var clean = function(item, retain){
    ....
    if (item.clearAttributes){
        var clone = retain && item.cloneNode(false);
        item.clearAttributes();
        if (clone) item.mergeAttributes(clone);
    } else if (item.removeEvents){          
    ....
};

You should be safe, it's emptying out the elements.

Also, credit for all code above to MooTools of course: http://mootools.net/

Nick Craver
Not if the browser supports it, but if the `item` sent to the `clean()` function does. `removeEvents()` is a Mootools function itself, not a builtin function.
Håvard S
@Håvard S - Actually I trimmed some logic out, it's wrapped in a `if (Browser.Engine.trident){` as well, basically an IE check...other browsers already remove events properly, this is an IE only memory leak band-aid. I changed the above "supports" to "needs"...hopefully a clearer answer.
Nick Craver
+1  A: 

Yes, Mootools will call removeEvents() when you call destroy() on an element.

(The current implementation does this in a function called clean() that is called from destroy()).

Håvard S