How to implement the OnDestroy / OnDispose event in JS / Mootools?

Is there an 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 the item is destroyed / deleted from the DOM. Something similar to this jQuery solution

+4
source share
1 answer

whereas you can do it, it’s impractical.

first - destroy- the event fills the fire with the context of the element being destroyed, at this point during the cb event it will be deleted and GCd, possibly.

second, IE6,7,8, , , $/document.id - , , , DOM.

-, , , el.parentNode.innerHTML = '' raw js/ . , 2 .

http://jsfiddle.net/5YYyb/1/

(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();
+4

Source: https://habr.com/ru/post/1540045/


All Articles