PDA

View Full Version : [CLOSED] TreeEdtior.cancelEdit does not revert to the old value



gr_great_2000
12 Jun 2010, 2:13 AM
Ext version tested:


Ext 3.2.0



Adapter used:


ext



css used:


only default ext-all.css



Browser versions tested against:


Firefox 3.6.3


Operating System:


Fedora 13
Windows Vista Home



Description:


TreeEditor.cancelEdit() does not revert to the old value, after an Ext.Ajax failure. In the code that follows, the first TreeEditor.cancelEdit works properly, while the second does not.



Test Case:



function updateHandler(editor, newVal, oldVal) {
//var node = tree.getSelectionModel().getSelectedNode();
var node = editor.editNode;
if (newVal == '') {
Ext.Msg.alert('Update failure', 'Empty values are not allowed');
// WORKS AS EXPECTED.
editor.cancelEdit(true);
} else if (newVal != oldVal) {
Ext.Ajax.request({
url: 'update_node.php',
method: 'POST',
params: {
node_id: node.id,
old_val: oldVal,
new_val: newVal
},
success: function(responseObject) {
Ext.Msg.alert("Status", responseObject.responseText);
},
failure: function() {
Ext.Msg.alert('Update failure', 'Error: Updating the database failed');
// DOES NOT WORK AS EXPECTED. DOES NOT REVERTED TO THE OLD VALUE.
editor.cancelEdit(true);
}
});
}
}


See this URL : http://gsiglet.dyndns.org:8080/tree/tree1.html


Steps to reproduce the problem:


Try to rename a node with a name that already exists (i.e. try to make a duplicate entry)



The result that was expected:


Duplicate entries are not allowed by the server. After an Ext.Ajax failure, I would expect the node to revert to the old value, since TreeEditor.cancelEdit(true) is called.
If -instead- you attempt to rename a node to an empty value, then TreeEditor.cancelEdit() works and the node reverts to the old value.



The result that occurs instead:


The old value is not reverted, although TreeEditor.cancelEdit(true) is called. What you see is duplicate node names, which is not allowed by the server.



Screenshot or Video:


Debugging already done:


none



Possible fix:


not provided

Other comments:


Plase forgive if this is not actually a bug, but I am relatively new to Extjs.

Condor
13 Jun 2010, 11:01 PM
No, this is not a bug.

Remember that Ext.Msg.alert is asynchronous. The updateHandler will already be finished and the tree node text updated before the callback will be executed.

To make this work you should use something like:

treeEditor.on('beforecomplete', function (editor, newVal, oldVal) {
if (this.allowComplete) {
delete this.allowComplete;
return true;
}
var node = editor.editNode;
if (!newVal) {
Ext.Msg.alert('Update failure', 'Empty values are not allowed');
} else if (newVal != oldVal) {
Ext.Ajax.request({
url: 'update_node.php',
method: 'POST',
params: {
node_id: node.id,
old_val: oldVal,
new_val: newVal
},
success: function (responseObject) {
Ext.Msg.alert("Status", responseObject.responseText);
this.allowComplete = true;
this.completeEdit(true);
},
failure: function () {
Ext.Msg.alert('Update failure', 'Error: Updating the database failed');
},
scope: this
});
}
return false;
}, treeEditor);

gr_great_2000
14 Jun 2010, 2:37 AM
Thank you very much for your reply.

What you are more or less suggesting is that the updateHanlder fuction should return false by default, and only in case of success, to complete the edit. Your approach does indeed solve the problem with the cancelEdit and the old value (in case of Ajax failure) is now reverted.

However, another problem has been created (unless I am not doing something right). In case of success, there is an infinite loop of update calls to the server (Maybe because the completeEdit is called, which also triggers again the 'beforecomplete' event- I am not sure).

I have found a way to overcome this problem. In the original code I posted, in case of Ajax failure, I do not call cancelEdit() directly (which I expected to work), but rather trigger a 'canceledit' event, and call a function that reverts the old value.

Thank you anyway,

Kind regards,
George

Condor
14 Jun 2010, 3:10 AM
Check my code again. I'm using the allowComplete flag to overcome the infinite loop.

gr_great_2000
14 Jun 2010, 4:01 AM
You are right :-)

Maybe it would be nice if -eventually- Extjs takes care of these loops automatically, without requiring to set flags (thus making things even simpler).

Regards
George