Reviewing the following code snippet (with some lines omitted for clarity), the editRow() function is triggered when a user clicks an edit icon, opening a modal window. Subsequently, the code attempts to save data every second by calling factory.submitItem. The process functions correctly except in cases where there is a failure in saving the data.
Is there a way to modify the code so that if factory.submit item entityResource.update encounters an error, the intervals are terminated and the continuous saving process is halted?
var factory = {
gridSetup: function ($scope) {
$scope.editRow = function (row, entityType) {
// modal functionality occurs here
window.setTimeout(function () {
window.setInterval(function () {
factory.submitItem($scope, $scope.modal.data);
}, 1 * 60 * 1000);
factory.submitItem($scope, $scope.modal.data);
}, 1 * 60 * 1000);
}
},
submitItem: function ($scope, formData) {
var idColumn = $scope.entityType.toLowerCase() + 'Id';
var entityId = formData[idColumn];
switch ($scope.modal.action) {
case "edit":
var entityResource = $resource('/api/:et/:id', { et: $scope.entityType }, { update: { method: 'PUT' } });
entityResource.update({ id: entityId }, formData,
function (result) {
angular.copy(result, $scope.modal.data);
}, function (result) {
// how should this be handled?
})
break;
}
},