View Full Version : Relying on success and failure handlers when saving a model object

24 Jul 2014, 9:32 AM
I'd like to ask a conceptual question about saving model objects in Ext JS 4.x.

Our application uses what is probably a very typical scenario: we load a model object into a form, then when the user clicks the Save button, we validate the data, update the model with the form's values, and call save() on the model to persist the data to the server. Before calling save(), we put up a "Saving..." mask over the entire Viewport, and we clear the mask in both the success() and failure() handlers of the save() call. My question is: can we trust that these handlers will only fire when they're supposed to?

The reason I ask is because I'm trying to debug a strange issue with our application where the user is somehow able to generate a 2nd request to save data before the 1st request is complete. Our server application is a Grails app, with an Apache web server in front of it, and we are not spawning our own threads or anything like that. The Apache access log shows the 1st PUT request, followed by an entry in our application log indicating entry into the server's controller method, and then the 2nd request shows up in the access log about 3 seconds after the first. (Irrelevant to my question but just to be complete: the 1st request eventually succeeds and the 2nd request fails with a StaleObjectException when it tries to write data that was changed by the 1st request).

So while I'm pretty sure I don't have to worry about dealing with potential double-clicks on a standard Ext JS button, I'm wondering if it's reasonable for me to rely on the success() and failure() handlers only firing after the server returns (or if the connection times out).

29 Jul 2014, 8:11 PM
You should be able to, yes.
Here is an example of saving a record instance. You can uncomment out the timeout to see what happens in the event of a transaction timing out or change the JSON response's success value to true/false to test.