PDA

View Full Version : beforeclose listener not waiting



mitchellsimoens
10 Feb 2010, 9:21 AM
I have a tab panel and have some panels set closable to true. I have a beforeclose listener on those panels. I can return false and it stops it but if I have an AJAX request running, the listener will go ahead and return true.

How can I stop this from returning before the AJAX request or if I need to prompt via a messagebox?

10 Feb 2010, 9:40 AM
I have a tab panel and have some panels set closable to true. I have a beforeclose listener on those panels. I can return false and it stops it but if I have an AJAX request running, the listener will go ahead and return true.

How can I stop this from returning before the AJAX request or if I need to prompt via a messagebox?

It should still return false.

Here's the flow that i would implement:

beforeclose->invoke ajax->return false;

ajax callback->if success->remove/destroy tab.

mitchellsimoens
10 Feb 2010, 9:55 AM
That's what I thought so I figured I was doing something wrong. Here is some code because I'm stupid on this one :)


listeners: {
beforeclose: function(t) {
if (t.isSaved) {
return true;
} else {
return confirmSaveFile(t);
}
}
}


var confirmSaveFile = function(t) {
if (t.title === t.initialConfig.title) {
Ext.MessageBox.prompt('Filename', 'Please enter a filename:', function(btn, text) {
if (btn === "ok") {
t.setTitle("*"+text);
return doFileSave(t);
} else {
return false;
}
});
} else {
return doFileSave(t);
}
}

var doFileSave = function(t) {
var projectName = t.ownerCt.project;
var source = t.items.items[0].getValue();
Ext.Ajax.request({
waitMsg: "Please wait...",
url: "functions.php",
method: "POST",
params: {
task: "SAVE-FILE-SOURCE",
project: projectName,
name: t.title.replace("*", ""),
source: source,
isNew: t.isNew
},
success: function(response){
var result = eval("("+response.responseText+")");
console.log("got it back");
if (result.success) {
t.setTitle(t.title.replace("*", ""));
t.isSaved = true;
return true;
} else {
Ext.MessageBox.alert("Error", result.msg);
return false;
}
},
failure: function(response){
Ext.MessageBox.alert("Error", "Could not find the server");
}
});
}

So when it gets to the confirmSaveFile function, it'll either prompt for a filename or execute doSaveFile. If it prompts, the prompt shows but I can see in the background the tab getting destroyed. Same thing for the AJAX call.

mystix
10 Feb 2010, 10:43 AM
as @jay has already mentioned, the trick to preventing a tab from closing is to always return false in the beforeclose (or beforeremove) event, then worry about dealing with programatically closing the tab based on the results of some decision -- in your case, the results of the AJAX call.

i.e.


listeners: {
beforeclose: function(t) {
if (!t.isSaved) {
confirmSaveFile(t);
}

// prevent tab from closing if file has not been saved
return t.isSaved;
}
}


note: you'll need to adjust the rest of your methods accordingly to not return redundant true/false values.