在工作时遇到这样一个问题,在保存数据时,需要对保存的数据进行多次校验(与后台交互的校验),如果校验成功则执行保存操作,如果不成功则返回提示。话不多说,直接上代码
var checkFun1 = function () {
return $q(function (resolve,reject) {
$http.post('product/checkSellingPriceLessThanCostPrice',salePolicy).success(function (response) {
if(response.result === 'success'){
resolve(response.data);
}else{
reject();
}
});
});
};
var checkFun2 = function () {
return $q(function (resolve,reject) {
$http.post('product/checkSellingPriceMoreThanGuidePrice',salePolicy).success(function (response) {
if(response.result === 'success'){
resolve(response.data);
}else{
reject();
}
});
});
};
var checkFun3 = function () {
return $q(function (resolve,reject) {
$http.post('product/checkSellingPriceOutOfPriceRange',salePolicy).success(function (response) {
if(response.result === 'success'){
resolve(response.data);
}else{
reject();
}
});
});
};
// 注意:城管提交时,如果不满足条件则不能提交,系管不满足条件只用提示即可,仍然可以提交
$q.all([checkFun1(), checkFun2(), checkFun3()]).then(function (data) {
var invalidNum = 0;
//data存储的时校验返回的信息
data.forEach(function (item) {
if(item){
invalidNum++;
}
});
if(invalidNum === 0){//当校验全部通过时执行保存操作
$scope.modifyProductSubmit(salePolicy);
}
});