2015-10-19 65 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(); 
     }); 
    } 
相關問題