键内部For循环是相同的内部然后功能

时间:2015-05-26 19:30:16

标签: angularjs

$scope.expandPackage = function (node, expanded) {
        $scope.expansion=expanded;

        var promises = [];

        //Check that expand command was clicked and the node's children were not already fetched
        if (expanded && !node.childrenFetched) {

            //console.log("Call to fetch children");
            //node.children = fetchChildren(node.name);
            var promise=getPackageHierarchyByPackageId(node.packageId).then(function(){
                if( $scope.packageEnabled) {
                     var featureinstancePromise=FeatureInstanceService.getfeatureInstanceForOrgUnitId(myRow.orgUnitId);
                     featureinstancePromise.then(function (featureInstance) {
                        featureInstanceList=featureInstance;
                    }).then(function(){
                        prepareListOfFeatureInstance(featureInstanceList,featureList);
                         for (var key in featureAndFeatInstanceIdMap) {
                            if (featureAndFeatInstanceIdMap.hasOwnProperty(key)) {
                                console.log(key + " -> " + featureAndFeatInstanceIdMap[key]);
                               var enablementPromise=FeatureInstanceService.getEnablementInfo(key);
                                promises.push(enablementPromise);
                                enablementPromise.then(function(enablementInfoSuccessResponse){
                                     $scope.featureAndEnablementInfo[featureAndFeatInstanceIdMap[key].displayName]=enablementInfoSuccessResponse;
                                });
                            }
                        }
                         $q.all(promises).then(function(){
                             $scope.featureList=featureList;

                         });
                     });

                }
            });

            node.childrenFetched = true;
        }

    };
this.getEnablementInfo = function(featureInstanceId) {
        var defer = $q.defer();

        $http.get(baseUrl + 'featureInstances/'+featureInstanceId +'/enablementInfo') .
            success(function(data, status, headers, config) {
                defer.resolve(data);
            }).error(function(data, status, headers, config) {
                console.log("Error message: " + data.message);
                defer.reject(data);
            });
        return defer.promise;
    };

执行then函数时,它总是返回相同的键,但enablementInfoSuccessResponse没问题。我的问题是我怎么知道哪个键的响应是什么?为什么键总是一样的?我怎么摆脱这个问题?

1 个答案:

答案 0 :(得分:1)

密钥始终相同,因为它的范围限定为解析then后调用的featureinstancePromise函数。由于存在用于处理异步代码的promise,for循环继续迭代,而不关心promise何时解析。这很好,但你不能依赖key是相同的,因为它在每个循环上都会更新。

有几种方法可以解决这个问题,但最简单的方法是将循环中的逻辑移动到一个函数中并传入密钥。这样,键将限定为该功能,并且不会在您下面发生变化。

function loopWork(key) {
  if (featureAndFeatInstanceIdMap.hasOwnProperty(key)) {
    console.log(key + " -> " + featureAndFeatInstanceIdMap[key]);
    var enablementPromise=FeatureInstanceService.getEnablementInfo(key);
    promises.push(enablementPromise);
    enablementPromise.then(function(enablementInfoSuccessResponse){
      $scope.featureAndEnablementInfo[featureAndFeatInstanceIdMap[key].displayName]=enablementInfoSuccessResponse;
    });
  }
}

prepareListOfFeatureInstance(featureInstanceList,featureList);
for (var key in featureAndFeatInstanceIdMap) {
   loopWork(key);
}