PDA

View Full Version : How does the Ext.dataOperation determine success or failure?



sg707
3 Mar 2011, 9:00 AM
When I invoke



newUser.save( {
success : function(user, operations) {
console.log(operations.success);
}

});
with the POST output



{
"errors":{
"name":"invalid name"
},
"success":false,
"msg":"my message"
}

In theory, "success" callback should not be executed right? However, it does and when I print out the Operation.success..the value was true

I can throw in HTTP Status code but in this case it's not a system error, therefore I still want to use HTTP 200 as a response.

Any help would be appreciated! :D

sg707
3 Mar 2011, 9:31 AM
I did find a workaround...but doesn't seem like a optimal solution....



newUser.save( {
success : function(user, operations) {
var response = Ext.decode(operations.response.responseText);
console.log(response.success);
if (!response.success) {
// validation code
}
},
failure : function() {
console.log('critical system error');
}
});


above code will print out "false"

icflorescu
3 Mar 2011, 9:50 AM
I haven't actually tested this, but I wonder what happens if you respond with a 4xx status code.
It's not wrong to do so, in fact, in theory according to IEFT/RFC standards that's what you should do in such a situation (see here (http://en.wikipedia.org/wiki/List_of_HTTP_status_codes)). Only 5xx codes are about server system errors.

I may be wrong, but as far as I remember, the failure callback is executed when a status code other than 200 is received (and maybe on timeout, can't remember for sure).
I think the reason we have this "success" property in ExtJS is that not all server-side stacks/frameworks return the correct headers...

sg707
3 Mar 2011, 10:25 AM
Tried with 400 code and it does call "failure". However, I'm not able to capture the actual validation error message because my server auto generates default message.... One thing that's puzzling is

Operation.error is set to undefined yet Operation.exception is set to true.

If the exception is set to "true" where is the error? I believe error is set through Operation.setException (http://localhost:8080/extjs4/script/ext-4.0-pr2/docs/api/Ext.data.Operation.html#setException)()

icflorescu
3 Mar 2011, 10:46 AM
Interesting... Again, one of those things (http://www.sencha.com/forum/showthread.php?125369-Model.load-response-structure&p=576546#post576546) that call for an official guide of best practices on how server interactions should be performed with ExtJS ajax/rest proxies... :)