PDA

View Full Version : Rest Proxy exception - operation remains in batch



murrah
31 Jul 2012, 3:30 PM
Hi,

Using Ext.data.proxy.Rest on a TreeStore. When I create a new node using parentnode.appendChild() the request is POSTed to the server correctly. The data comes from a form panel.

node.appendChild({ // This sends a POST to the server
leaf: true,
//parentId: node.id, // assigned automatically
text: record.get('name'),
name: record.get('name'),
email: record.get('email'),
datejoined: record.get('datejoined'),
balance: record.get('balance'),
worksfor: node.data.id
});


In my server logic, if the model associated with the tree node is not allowed to be added (in this case the model has a duplicate email address), I return status 200, success=false. This fires the exception event. So far, so good, except that it seems odd that a success=false throws an exception even if the status code is 200. Perhaps this is part of my problem?

Anyway...
In my exception listener I handle all the exceptions. At this point the node has an internalId but no real id since the server rejected the request so has not allocated one. I need to remove the node from the tree since it is now "invalid" and can then allow the user to try again after explaining why the add was not successful (yes, using saved data ;) ).

I remove the node like so:
var phantomId = options.records[0].internalId;
if (phantomId) {
var tree = Ext.ComponentQuery.query('usertree')[0];
var store = tree.getStore();
var node = store.getNodeById(phantomId);
if (node) {
console.log('node '+phantomId+' removed')
node.remove(true);
}
}
Now, when I try to add again I notice that there was a POST of the original attempt as well as my new attempt. Inspecting the options.batch.operations array I see that the original attempt is still in there. It seems that because the exception was thrown the operation is not removed from the batch.

Should that happen automatically, or do I need to deal with removing the failed operation from the batch myself? Or am I going about this the wrong way altogether?

In what may be a related issue, the
node.remove(true); sends two DELETE operations to the server, both with no id (since there isnt one!). I catch them on the server OK but why is it sending 2 requests?

Thanks,
Murray