I'm setting a small window for set and update parameters of different modules in a web app, for this, I'm using a jQueryUI dialog that loads the settings from each module at the time. My problem is that if you hit the button cancel it will reset all the configuration to the initial state even if I have made changes before and saved them before opening the dialog again and cancel.
Also, I would like the Close 'X' button to have the same behavior as the Cancel button.
var appDiv = $(Container).find(".divAppAdmin")[0];
var backupDiv = appDiv.innerHTML;
var closedFunction = function() {
$(appDiv).dialog( "destroy" );
};
var closeFunction = function() {
$(appDiv).dialog( "close" );
$(Container).append(appDiv);
};
var cancelFunction = function() {
$(appDiv).dialog( "close" );
appDiv.innerHTML = backupDiv;
$(Container).append(appDiv);
}
$(appDiv).dialog({
"modal": true,
"title": "Edit",
"close": closedFunction,
"buttons": {
"Save": closeFunction,
"Cancel": cancelFunction
},
"minWidth": 600
});
Step by step:
1. Open the dialog and introduce some changes,
2. Save the changes,
3. Open the dialog again without making any changes,
4. Hit button Cancel.
At this point, I lose the changes I've made before and saved on step 2 and the module goes back to the state it was before step 1.
I know that using
appDiv.innerHTML = backupDiv;
is causing the loss of the changes in this specific situation but it works fine if I actually want to undo any modifications.
EDIT:
I kept digging and so far I found a solution for copying the Cancel behavior to the Close 'X' button (How to find out if jquery dialog was closed on escape and execute some code):
var closedFunction = function (event, ui) {
if (event.originalEvent) {
cancelFunction();
}
$(appDiv).dialog("destroy");
};
By adding this if clause in the closedFunction() allows me to capture when the dialog is being closed by hitting the 'X' button or pressing the ESCAPE key, then I can call the cancelFunction() inside the if and now is the same as if hitting the Cancel button.