• 处理异步问题


    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);
        }
    };

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

  • 相关阅读:
    Wijmo 更优美的jQuery UI部件集:在对Wijmo GridView进行排序或者过滤时保留选择
    Wijmo 更优美的jQuery UI部件集:活动日历控件(Event Calendar)
    Wijmo 更优美的jQuery UI部件集:导出Wijmo的GridView到Excel
    Wijmo 更优美的jQuery UI部件集:服务器端Grid魔法
    Wijmo 更优美的jQuery UI部件集:在安全站点使用Wijmo控件
    Wijmo 更优美的jQuery UI部件集:复合图表(CompositeChart)
    Wijmo 更优美的jQuery UI部件集:C1 Wijmo Grids 更多惊喜
    Hello Metro:Windows 8下首个App
    Wijmo 更优美的jQuery UI部件集:运行时处理Wijmo GridView数据操作
    Wijmo 更优美的jQuery UI部件集:自定义 C1WijMenu
  • 原文地址:https://www.cnblogs.com/ms-grf/p/7249833.html
Copyright © 2020-2023  润新知