原逻辑:
保存=>验证
新需求:
保存=>验证=>验证2(异步1=>异步2=>异步3=>验证结果)
新逻辑:
验证=>前验证=>验证2-异步1=>验证2-异步2=>验证2-异步3=>后验证=>保存
新逻辑实现:
验证
$scope.checkDoctorOrders = function () {$scope.R1007isPassed = 0;//默认未通过var p = new Promise((resolve,reject)=>{ resolve()})p.then($scope.checkDoctorOrdersPre) }
前验证
$scope.checkDoctorOrdersPre = function(){...$scope.CheckisPassed = true;var p = Promise.resolve().then($scope.platformConfirm_R1007_sub1)console.log("checkDoctorOrdersPre done");return p;}
验证2-异步1
$scope.platformConfirm_R1007_sub1 = function(){var registerId = $scope.curPatient.RegisterId;var subUrl = "Register/GetRegisterRecord";var regData = { RegisterId: registerId }Http.requestData({ url: subUrl, data: regData }, function (data) { var p = Promise.resolve(data).then($scope.platformConfirm_R1007_sub2)console.log("platformConfirm_R1007_sub1 done")return p;}, function (data) { });}
验证2-异步2
$scope.platformConfirm_R1007_sub2 = function(data){...var subData = {sysCode: "R1007",method: "DuplicateItemVerify",UserInfo: "访问接口账号",data: sendData,}; var jsbody = JSON.stringify(subData)var appendurl = "?strJson="+jsbodyHttp.requestData({url: subUrl+appendurl}, function (repdata) {console.log(repdata.respCode); }, function (repdata) { var p = Promise.resolve(repdata).then($scope.platformConfirm_R1007_sub3)console.log("platformConfirm_R1007_sub2 done")return p;});}}
验证2-异步3
$scope.platformConfirm_R1007_sub3 = function(repdata){if (repdata.respCode == "10000") { if (repdata.respData.IsRepeat == 1) { var popUrl = repdata.respData.PromptUrl;console.log(popUrl);window.open(popUrl);}else{$scope.R1007isPassed = 1;//设置通过console.log($scope.R1007isPassed); var p = Promise.resolve().then($scope.checkDoctorOrdersAfter) console.log("platformConfirm_R1007_sub3 done")return p;}}}
后验证
$scope.checkDoctorOrdersAfter = function(){ if ($scope.R1007isPassed == 0) {$scope.CheckisPassed = false;}console.log("checkDoctorOrdersAfter done");if ($scope.CheckisPassed ==true){//调用保存方法$scope.saveDoctorItem();}}
保存
$scope.saveDoctorItem = function () {//if (!$scope.checkDoctorOrders()) return false;...$scope.saveInPatientOrder();}
总结:
关键还是思路的转变,之前是验证由保存触发,调整为将保存作为链式调用的最后一个节点