marko-js-archive / marko-widgets

[LEGACY] Module to support binding of behavior to rendered UI components rendered on the server or client
http://v3.markojs.com/docs/marko-widgets/
MIT License
142 stars 40 forks source link

Add support for delaying widget destruction from an onBeforeDestroy handler #157

Closed colin-aarts closed 7 years ago

colin-aarts commented 7 years ago

Having a mechanism for delaying destruction of a widget from a widget's onBeforeDestroy (or similar) callback would be useful for a number of cases, such as easily running teardown animations, without having to micro-manage that from outside. E.g, based on Glitter discussion:

onBeforeDestroy(event) {
    var destroy = event.delayDestroy();
    this.el.addEventListener('transitionend', function() {
        destroy();
    });
    /* animate el */
}
CestDiego commented 7 years ago

This would be very helpful to have

patrick-steele-idem commented 7 years ago

This is actually a feature of Marko v4, but I'm realizing it is not documented anywhere. Marko v4 supports on-attach and on-detach events for any DOM elements that can be used to animate in and animate out DOM elements from the DOM. You can see it in action here: http://markojs.com/try-online/?file=%2Ftodomvc%2Fcomponents%2Ftodomvc-todo-item%2Fdemo.marko&gist=

Calling event.preventDefault() in the on-detach event handler prevents an element from being removed from the DOM. After the animation has been completed you should call event.detach(); to complete the removal of the DOM element out of the DOM.

I'll open an issue on the main marko repo to document this feature.