Re-implement Element.dispose?

23 views
Skip to first unread message

Regis

unread,
Feb 3, 2012, 3:15:35 PM2/3/12
to mootool...@googlegroups.com
So, I need some help. I need to run some code on an element and all of it's children when it's removed from the DOM via Mootools. destroy(), empty(), dispose(), etc. Since all of these end up calling dispose in the end, this is the method I would like to reimplement. I was trying to do something like:

Element.implement('mooDispose'Element.prototype.dispose);
Element.implement('dispose'function(){
    /* my code */
    this.mooDispose();
});


And this seems works fine when I call dispose on the element as $(el).dispose();  However, when I call $(el).destroy() which calls Element.dispose(this); it does not call my dispose, but rather the original dispose, thus going around my implementation. Is there a way I'm missing so Element.dispose calls my implementation of it? In the implementation method in Mootools, Element.dispose(this); should call my function with apply(el) from the, but something else must be happening I don't quite understand. I feel so close... 

Here's a jsfiddle. Notice that dispose calls mine, but destroy or empty does not: http://jsfiddle.net/rgthree/QG7Fa/

Thanks!


Aaron Newton

unread,
Feb 3, 2012, 7:26:44 PM2/3/12
to mootool...@googlegroups.com
Element.mooDispose = Element.dispose;
Element.dispose = function(element){
  /* your code */
  Element.mooDispose(element);
});

This kind of hackery may, actually, will *probably* cause you headaches at some point.

Regis

unread,
Feb 8, 2012, 12:34:00 PM2/8/12
to mootool...@googlegroups.com
Thanks Aaron, I could have sworn i tried that.

I'm saddened that this will *probably* cause headaches. While I can certainly see the reasons why this could be dangerous, I would hope that this would be clean enough (my code isn't manipulating the element in question at all). Unless there's something I don't know about, there' no other way to detect when an element is being removed from the DOM, is there?

Aaron Newton

unread,
Feb 9, 2012, 1:22:03 PM2/9/12
to mootool...@googlegroups.com
No. My other caution is relative to performance; when you remove a DOM tree you might delete hundreds or even thousands of nodes.
Reply all
Reply to author
Forward
0 new messages