We have a web portal, when user submits a form, after filling all mandatory details, the UI is posting null / empty value for one of the mandatory field – “Name” to the server API.
Name is a textbox (a dojo widget), it has onFocusOut()
method handled. This method performs some checks / serialization / de-serialization and asynchronously updates Name property of main object say "employee".
There is a submit()
method, that is triggered on click of Submit button on this form. This method posts employee object as payload to server API.
ISSUE:
When user types in the name textbox, and without loosing focus clicks on Submit button.
This triggers two async methods (onFocusOut()
and submit()
methods) separated by just a fraction of milli second.
In some rare scenarios, submit()
is posting the employee object to server API, before the Name property of employee object is updated by onFocusOut()
method.
SOLUTION?
Here are different possible solution to this issue –
- Using
setTimeout()
to delay triggering ofsubmit()
method. - Update the model with
onChange()
instead ofonFocusOut()
of text box. - Make a function call to check all validations immediately before
submit()
method API post. - Use a class level flag. Set it on/off at start and end of
onFocusOut()
methods. Submit waits for this flag to be set, and then executes API post.
What is the optimum solution for such RACE condition? What are the best practices to handle such situation in dojo application?