I have made a generic function to show jQuery dialogs in my app. this function simply
- Set the dialog title
- make an ajax call to a server side function and populate the dialog with the html returned
- open the dialog
Here is the function:
function loadDialog(action, id, title, onCloseHandler) {
$("#__gsl_DialogPanel").dialog("option", "title", title);
var url = action;
if (id != "") url = url + "/" + id;
$.ajax({
type: "get",
dataType: "html",
url: url,
data: {},
success: function(response) {
$("#__gsl_DialogPanel").html('').html(response).dialog('open');
}
});
$("#__gsl_DialogPanel").bind("dialogclose", function(event, ui) {
if (onCloseHandler != null)
onCloseHandler();
});
}
As you can see the function actually does even bind a function to the dialogclose
event so when the dialog is closed that function get called. This feature is particularly useful if I use a dialog to add/edit/delete elements in a grid: I can pass a function that does a grid update and when the dialog is closed the grid will update itself.
I am experiencing that, in some cases, the function binded to the dialogclose
event is called more than once and I am suspecting that the problem is related to the fact that multiple calls to my loadDialog
function will bind multiple reference of the same close handler.
Am I correct? Do I have to explicitly unbind everything whn the dialog close?
thanks for helping!
Update:
As per the answers received I have simply unbinded and binded again the function handler and everything works! this is the updated code
$("#__gsl_DialogPanel").unbind("dialogclose").bind("dialogclose", function(event, ui) {
if (onCloseHandler != null)
onCloseHandler();
});
}