I had to change the original flow, basically move it and chain to the Async call's onSuccess().
Originally, the flow is
- user click a poopup dialog's Ok button
- validate (everthing is front end validation), if on validation error, throw exception and show error. diaglog is not closed.
- On success validation, proceed with data processing and close the dialog.
Now In a new scenaro, the #2 validate is changed to require a Async call back to the back end. So #3 has to be moved to chain to the callback on the validate method. '
Code snippets below
public void onValidationDataReady(List<Long> existingTests) throws ValidationException {
if (!existingTests.isEmpty()) {
throw new ValidationException("The entered name has already been used.");
}
//only after the validation do we proceed with the original OK click
proceedOkClick(testNameField.getValue());
}
public void proceedOkClick(String data) {
// proceed only after the async call
if (callback != null) {
callback.onDialogResult(true, data);
}
clearDialog();
}
public boolean validateInputs() throws ValidationException {
//async call to get data from back end.
//pass in a Callback
testNameValidator.validate(testNameField.getValue(), new DataReadyCallback<List<Long>>() {
@Override
public void onDataReady(List<Long> existingTests) {
onValidationDataReady(existingTests);
}
});
return true;
}
//The call back interface.
public interface DataReadyCallback<T> {
void onDataReady(T data);
}