The code below doesn't seem right to me. The scenario is:
- User enters in a "quantity" into a form field.
- If the user enters in 5, the loop below should iterate 5 times.
- There should now be 5 new entries in the database with the
container.product
andcontainer.status
.
Any advice on making this cleaner?
var container = new Container($scope.batchFields);
container.product = $scope.item.originalObject._id;
container.status = 'active';
_.each($scope.batchFields, function(batch) {
_.each(_.range(batch.batchesQuantity, 0, -1), function (val,i) {
// This $save code smells a little with it
// being inside the _.each.
container.$save(function(response) {
$notify.success('purchase.order.edit.created');
if ($scope.$close) {
$scope.$close();
}
}, function(errorResponse) {
$scope.errors = errorResponse.data.message;
});
});
});