Search code examples
javascriptjqueryjquery-uiinheritanceextending

jQuery UI event functionality extending


I want to add functionality of auto-destroying dialog on close without adding any special code to every dialog call in the current project. So I think it needs to override the default dialog close event.

I found a way to do this (for example: How to extend a jquery ui widget ? (1.7)), but I don't want just override the event: I also need to save the previous behavior of the event and add $(this).dialog("destroy") call after it.

Any suggestions?


Solution

  • I'm not 100% sure about the correctness of this, but I think you can safely override the close method like this:

    $.ui.dialog.prototype._originalClose = $.ui.dialog.prototype.close;
    $.ui.dialog.prototype.close = function() {
            alert ('My stuff');
            $.ui.dialog.prototype._originalClose.apply(this, arguments);
    };
    

    You can see this working at: http://jsfiddle.net/8KKMm/

    However, it's normally a good idea to avoid overriding external libraries. There might be better ways to achieve your target without mangling with jQuery UI library. Please do have a look at the available events of the Dialog component: http://jqueryui.com/demos/dialog/.