处理异步问题

1、for循环中处理异步问题(调用查询数据库的耗时操作)

错误代码:(下面这个代码如果执行后,会出现查询出来的值赋值混乱的现象)

if ($scope.isOther(orderClass)) {
    getStartAndEndDateTime(data.object.packBeginDay, data.object.packEndDay, function (startDateTime, endDateTime) {
        for (var i in cpExamLabMasterList) {//for循环中查询数据库的操作
            cpExamLabMasterList[i].orderClass = orderClass;
            var id = getId();
            id = packOrderDetail.id + id;
            cpExamLabMasterList[i].startDateTime = startDateTime;
            cpExamLabMasterList[i].endDateTime = endDateTime;
            $scope.cpOrderTreatList.push(cpExamLabMasterList[i]);
            var orderDetail = getOrderDetailTreat(cpExamLabMasterList[i], id, packOrderDetail.id);
            orderDetail.performDeptList = [];
            /*处置|其他执行科室   查询数据库耗时操作*/
            if(cpExamLabMasterList[i].cpTreatItemList.length > 0){
                var successCallback = function (data) {
                    orderDetail.performDeptList = angular.copy(data);//把查询出来的数据赋值给下标对应的对象
                };
                HrUtils.httpRequest($http, Path.getUri("api/treat-dict/perform-dept/"), successCallback , HrUtils.httpMethod.GET);
            }
            checkSelectAttr(packOrderDetail,orderDetail,i);
            $scope.cpOrderDetailList.push(orderDetail);
        }
    });
}

正确代码:

if ($scope.isOther(orderClass)) {
    getStartAndEndDateTime(data.object.packBeginDay, data.object.packEndDay, function (startDateTime, endDateTime) {
        var aysncInfo = {index: 0, count: cpExamLabMasterList.length};
        refreshTreatWithPerformDept(orderClass, startDateTime, endDateTime, packOrderDetail, cpExamLabMasterList, aysncInfo);
    });
}

var refreshTreatWithPerformDept = function (orderClass, startDateTime, endDateTime, packOrderDetail, cpExamLabMasterList, aysncInfo) {
    if (aysncInfo.index === aysncInfo.count) {
        return;
    }
    var cpExamLabMaster = cpExamLabMasterList[aysncInfo.index];
    cpExamLabMaster.orderClass = orderClass;
    var id = getId();
    id = packOrderDetail.id + id;
    cpExamLabMaster.startDateTime = startDateTime;
    cpExamLabMaster.endDateTime = endDateTime;
    $scope.cpOrderTreatList.push(cpExamLabMaster);

    var orderDetail = getOrderDetailTreat(cpExamLabMaster, id, packOrderDetail.id);
    checkSelectAttr(packOrderDetail, orderDetail, aysncInfo.index);
    $scope.cpOrderDetailList.push(orderDetail);
    aysncInfo.index++;
    if (orderDetail.itemCode && (orderClass === orderClassEnum.orderTreat || orderClass === orderClassEnum.orderNurse)) {
        var successCallback = function (data) {
            orderDetail.performDeptList = angular.copy(data);
            refreshTreatWithPerformDept(orderClass, startDateTime, endDateTime, packOrderDetail, cpExamLabMasterList, aysncInfo);
        };
        var errorCallback = function () {
            refreshTreatWithPerformDept(orderClass, startDateTime, endDateTime, packOrderDetail, cpExamLabMasterList, aysncInfo);
        };
        HrUtils.httpRequest($http, Path.getUri("api/treat-dict/perform-dept/"), successCallback, hrDialog, errorCallback, HrUtils.httpMethod.GET,);
    } else {
        refreshTreatWithPerformDept(orderClass, startDateTime, endDateTime, packOrderDetail, cpExamLabMasterList, aysncInfo);
    }
};

---------------------------------------------------------------------------------------------------

posted @ 2017-07-28 13:55  发福大叔  阅读(452)  评论(0编辑  收藏  举报