I have the following loop that checks through each row in a grid and then saves the rows that have been changed:
for (var i = 0, len = $scope.grid.data.length; i < len; i++) {
if (!angular.equals($scope.grid.data[i], $scope.grid.backup[i])) {
var rowData = $scope.grid.data[i]
var idColumn = $scope.entityType.toLowerCase() + 'Id';
var entityId = rowData[idColumn];
var entityResource = $resource('/api/:et/:id', { et: $scope.entityType }, { update: { method: 'PUT' } });
$scope.grid.showMessage = true;
$scope.grid.message = 'Saving Id: ' + entityId + ' ...';
entityResource.update({ id: entityId }, $scope.grid.data[i],
function (result) {
angular.copy(result, $scope.grid.data[i]);
angular.copy(result, $scope.grid.backup[i]);
$timeout(function () {
$scope.grid.showMessage = false;
$scope.$broadcast('gridSetPristine');
}, 1000);
}, function (result) {
var msg = result.data.exceptionMessage;
$scope.grid.message = "Error saving grid data";
$timeout(function () {
$scope.grid.showMessage = false;
}, 5000);
})
}
}
This works good if all the rows save okay. However I would like to change it so it stops if one of the saves fails.
The problem I have is that the update functions do not return until later. How can I make it do the update, wait for the result and then either continue or break out of the for loop if there is an error? Note that I am using 1.2 RC3 version of Angular. Also I do not mind that this may be slow as usually there will be only less than three rows that have been updated in my grid each time the save button is pressed which then calls the code in the for loop.