2015-10-19 21 views
1

当用户单击表单上的按钮时,我希望它禁用所有控件,然后转到服务器以执行操作,因此它们不能在更改结果之前更改任何值背部。当结果返回时,表单可以重新启用。角色绑定在阻止调用之前不会触发

然而,这是我的代码

$scope.restartNode = function() { 
      $scope.isBusy = true; 
      $scope.disableControls = true; 

      SEApplicationService.restartNode(); 

      $scope.isBusy = false; 
      $scope.disableControls = false; 
     } 

的restartNode是阻塞调用到Web API方法,但是管制没有得到禁止。如果我注释掉最后一行 - 将控件重新设置为启用,我可以看到所有表单控件都按预期变灰。这里发生了什么?

我的阻塞调用是

appService.restartNode = function() { 
     return $http.post("/SEFlex/SEFlexAdmin/RestartNode", { 
      isPrimary: appService.primarySelected 
     }).success(function (response, status, headers, config) { 
      appService.refreshData(); 
     }).error(function (reason, status, headers, config) { 
      console.log(reason); 
     }); 
    } 

编辑:添加refreshData()函数

appService.refreshData = function() { 
     return $http.post("/SEFlex/SEFlexAdmin/GetNodesStatus") 
      .success(function (response, status, headers, config) { 
       if (response) { 

        angular.forEach(response.data, function(value) { 
         if (value.isPrimary) { 
          appService.ServiceStatus.PrimaryStatus = value.status; 
          appService.ServiceStatus.PrimaryPools = value.poolCount; 
          appService.ServiceStatus.PrimaryContainers = value.containerCount; 
          appService.ServiceStatus.PrimaryNodes = value.nodeCount; 
         } else { 
          appService.ServiceStatus.SecondaryStatus = value.status; 
          appService.ServiceStatus.SecondaryPools = value.poolCount; 
          appService.ServiceStatus.SecondaryContainers = value.containerCount; 
          appService.ServiceStatus.SecondaryNodes = value.nodeCount; 
         } 
        }); 
       } 
      }) 
      .error(function (reason, status, headers, config) { 
       console.log(reason); 
      }); 
    } 

回答

1

问题是请求是异步的,并且您不等待响应来更改您的busydisable变量。

总结他们到restart

$scope.restartNode = function() { 
     $scope.isBusy = true; 
     $scope.disableControls = true; 

     SEApplicationService.restartNode().then(function(){ 
      $scope.isBusy = false; 
      $scope.disableControls = false; 
     }).catch(function(err){ 
      // any promise rejected in chain will be caught here 
     });    
    } 

编辑的承诺回调:正确地创建的restartNode承诺链和refreshData你需要切换出successthen。这是一个原因success被弃用:

appService.refreshData = function() { 
    return $http.post("/SEFlex/SEFlexAdmin/GetNodesStatus") 
     .then(function (response, status, headers, config) { 
      var responseData = response.data; 
      if (responseData) { 

       angular.forEach(responseData.data, function(value) { 
        // code left out for brevity 
       }); 
      } 
     }); 
} 

然后你就可以在thenSEApplicationService.restartNode()

appService.restartNode = function() { 
    return $http.post("/SEFlex/SEFlexAdmin/RestartNode", { 
     isPrimary: appService.primarySelected 
    }).then(function (response, status, headers, config) { 
     return appService.refreshData(); 
    }) 
} 
+0

谢谢,then()方法会在异步调用的success()部分之前还是之后运行?即将appService.refreshData()运行并在then()承诺被调用之前完全完成? – NZJames

+0

是'appService.refreshData();'也是异步的?如果是这样,它会返回'$ http'?可以链接的承诺,如果是这样,但需要使用'然后'而不是'成功' – charlietfl

+0

刚编辑的帖子显示refreshData()函数 – NZJames

-1

把范围$适用()之后$ scope.disableControls =假。

0

的u可以使用回调函数,像这样

$scope.restartNode = function() { 
      $scope.isBusy = true; 
      $scope.disableControls = true; 

      SEApplicationService.restartNode(function(){ 

        $scope.isBusy = false; 
        $scope.disableControls = false; 

      }); 

     } 
返回 refreshData承诺

这里是服务器功能

appService.restartNode = function (callback) { 
     return $http.post("/SEFlex/SEFlexAdmin/RestartNode", { 
      isPrimary: appService.primarySelected 
     }).success(function (response, status, headers, config) { 
      appService.refreshData(); 
      callback(); 
     }).error(function (reason, status, headers, config) { 
      console.log(reason); 
      callback(); 
     }); 
    } 
相关问题