When an asynchronous call is made inside the if
block, it raises the question of how the else
statement block can be executed if doSubmit
returns true
. This often leads to reaching the line after the ERROR_2
comment:
$scope.save = function() {
if (doSubmit) {
var dto = {
'attributes': $scope.fields,
'requestOrigin': $location.absUrl()
};
var req = {
method: 'POST',
url: 'endpoint',
data: dto
};
$http(req).success(function(data, status) {
// SUCCESS
$scope.completed(data);
}).error(function(data, status) {
// ERROR_1
$scope.validationFailed();
});
} else {
// ERROR_2
$scope.validationFailed();
}
}
// Function for displaying error messages related to validation failures
$scope.validationFailed = function(message, id) {
$scope.alerts.push({
type: 'danger',
msg: message || 'error.validation_failed', id: id || 'general'
});
}
I recall reading about this specific scenario somewhere, but I cannot seem to remember the source. Any additional information or a brief explanation would be appreciated.
ADDITION 1: $scope.validationFailed