0

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?

Community
  • 1
  • 1
User
  • 111
  • 1
  • 11
  • You must save a destroy handler first, that run your own, and then saved one. How do you manage to override this handler? – kaz Jan 03 '12 at 12:10

2 Answers2

3

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/.

jjmontes
  • 24,679
  • 4
  • 39
  • 51
  • Thanks, works fine. And, yes, it will be better to use events. Anyway, also would like to know how to call inside overrided method 'destroy' or 'open' of self? TIA. – User Jan 03 '12 at 12:37
  • 1
    I think you can simply call `this.destroy()` within that context. Have a look at http://jsfiddle.net/8KKMm/2/ . – jjmontes Jan 03 '12 at 12:49
1

You can add a dialogclose handler to the body element of the page.

You can find a sample here.

There is no need to override the close function of the dialog class, you can simply use the events provided by the dialog class.

Ex:

$("body").on("dialogclose", function(){
    alert("closed");
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531