Is there any existing OnDestroy/OnDispose event in JavaScript or are there any known custom implementations in plain JS or Mootools? Let's say I want to call console.log('bye') when an element gets destroyed/removed from the DOM. Something similar to this jQuery solution
Asked
Active
Viewed 5,355 times
1 Answers
4
whereas you can do this, it's not practical to do so.
first - destroy
- the event fill fire with the context of the element that is being destroyed, at which point during the event cb, it will get removed and GCd, potentially.
second, IE6,7,8 where Element prototype is read-only and elements get the methods added to them locally via the $
/document.id
- means that the decorated methods need to be loaded before anything is accessed in the DOM.
third, this won't actually fire if say, el.parentNode.innerHTML = ''
or they get removed via raw js / alternative ways. it's not a true watcher in that sense, just traps 2 methods.
(function(){
// old methods
var destroy = Element.prototype.destroy,
dispose = Element.prototype.dispose;
// redefine them and fire the events before calling old protos.
[Element, Elements].invoke('implement', {
destroy: function(){
this.fireEvent('destroy');
return destroy.apply(this, arguments);
},
dispose: function(){
this.fireEvent('dispose');
return dispose.apply(this, arguments);
}
});
}());
var foo = document.getElement('.foo');
foo.addEvents({
dispose: function(){
alert('we are not in the dom now');
}
});
foo.dispose();

Dimitar Christoff
- 26,147
- 8
- 50
- 69
-
Thanks for a great answer and the fiddle. First point: does it mean the event can be destroyed before it is fired? Would you also mind further explaining your second point? What are the decorated methods and why/how to load them? – Slava May 10 '14 at 16:32
-
1so. in normal browsers, you can extend `Element.prototype`. eg. `Element.prototype.foo = 'bar'; var div = document.createElement('div'); div.foo; // bar` - not in old IE. Since mootools is prototypal and so is javascript, it uses a trick--when an element is touched in IE, it copies the extended Element prototype methods onto the element object directly, which then allows you to call `el.foo` w/o the property resolution from the proto as it will have an own property instead. as for decorated methods, this is standard overloading - modified functions do something on top of old func then call old – Dimitar Christoff May 10 '14 at 18:12
-
1so to explain above, we save the old ref, create a new function that does something different and eventually call the old original method--which is basically decorating the original with extra functionality. for example, `Function.prototype.bind` does similar thing, takes the old function, keeps it inside a wrapper func and applies the scope passed to keep context as needed. etc etc. as for point one, the event will fire but it will reference an element that is being destroyed - depends what you use it for - if async, may be probs. also, references to el itself there may affect GC – Dimitar Christoff May 10 '14 at 18:14
-
For others interested: you may want to fire this event for all children of the destroyed element. [Fiddle update](http://jsfiddle.net/5YYyb/3/) – Slava May 11 '14 at 12:47
-
1an idea to do stuff via http://jsfiddle.net/dimitar/PYErL/, the observable api (modern browsers) – Dimitar Christoff May 12 '14 at 08:29