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?

1
  • You must save a destroy handler first, that run your own, and then saved one. How do you manage to override this handler? Commented Jan 3, 2012 at 12:10

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
I think you can simply call this.destroy() within that context. Have a look at jsfiddle.net/8KKMm/2 .
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");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.