0

我工作我的angularjs項目。 我創造了這個服務:Http服務延遲處理,直到返回數據後

(function() { 
    "use strict"; 

    angular.module("manageItems").factory("manageItemsService", ["$http", "config", manageItemsService]); 

    function manageItemsService($http, config) { 
     var service = { 
      getNewItems: getNewItems, 
     }; 
     return service; 

     function getNewItems(session, mapName) { 
      return $http.get(serviceUrl + 'getNewItems/' + session + "/" + mapName); 
     } 

    } 
})(); 

在這裏,我如何調用該服務控制器:

function getNewItems() { 
    manageItemsService.getNewItems(mapguideService.mapName, mapguideService.sessionId).then(function (result) { 
     self.currentItems = result.data; 
    }) 
} 

我需要的服務延遲,而返回的響應。

如何更改servicefunction以使其等待self.currentItems屬性由數據填充?

+1

這是預期的行爲,但問題是什麼? –

+0

@PankajParkar請參閱更新 – Michael

+0

我可以知道什麼是確切的情況? –

回答

0

然後你可以把.then放在getNewItems $ http調用。並基於檢索到的響應數據,決定是返回數據還是調用其他服務方法。

function anotherFunction(){ 
    return $http.get(url); 
} 

function getNewItems(session, mapName) { 
    return $http.get(serviceUrl + 'getNewItems/' + session + "/" + mapName).then(function successCallback(response){ 
     var data = response.data; 
     //call another function if data is empty 
     if(!data.length) 
     return anotherFunction(); //make sure another function should return promise 
     return data; 
    }); 
} 
+0

感謝post.What如果我需要打3個電話?我的意思是如果第一個是空的第二個如果seci = ond是空的第三個。我不認爲這是好的做法 – Michael

+2

這是我如何處理它,當我想要在服務響應之前處理中間步驟。使用和擴展承諾鏈接是處理這種情況的好方法。我不認爲這是它的壞習慣(讓我知道,如果你發現任何參考,其狀態提到是不好的做法),謝謝;) –

1

首先,我需要說http請求實際上是異步執行的,以便在返回結果時不停止應用程序。

所以你有兩個選擇,使用角度模式來調整你的方法來處理結果,所以你必須傳遞一個回調函數到服務,以便服務,而不是控制器使關聯。這將是這樣的:

服務:

(function() { 
     "use strict"; 

     angular.module("manageItems").factory("manageItemsService", ["$http", "config", manageItemsService]); 

     function manageItemsService($http, config) { 
      var service = { 
       getNewItems: getNewItems, 
      }; 
      return service; 

      function getNewItems(session, mapName, callback, errorCallback) { 
       $http.get(serviceUrl + 'getNewItems/' + session + "/" + mapName).then(callback, errorCallback);; 
      } 

     } 
    })(); 

控制器:

function getNewItems() { 
    manageItemsService.getNewItems(mapguideService.mapName, mapguideService.sessionId, function (result) { 
     //this callback will be called asynchronously when the response is available 
     self.currentItems = result.data; 
    }, function(error) { 
     // called asynchronously if an error occurs 
     // or server returns response with an error status. 
    }) 
} 

的第二個選擇是完全不推薦,插入一個循環,而結果有望...(以不好)

我希望我能幫上忙!

+1

請參閱[爲什麼是從承諾回調'.then'方法反模式](http ://stackoverflow.com/questions/35660881/why-are-callbacks-from-promise-then-methods-an-anti-pattern)。 – georgeawg

0

代碼需要做的是承諾。

爲了使getNewItems功能可鏈接,回報派生承諾:

function getNewItems() { 
    //vvvv RETURN promise 
    return manageItemsService.getNewItems(mapguideService.mapName, mapguideService.sessionId) 
     .then(function (response) { 
     self.currentItems = response.data; 
     //RETURN value to chain 
     return response.data; 
    }); 
}; 

然後使用返回的承諾更多的操作:

getNewItems().then(function(currentItems) { 
    //Evaluate current Items 
    if (ok) { 
     return "DONE"; 
    } else { 
     //RETURN to chain something else 
     return getOtherItems(); 
    }; 
}).then(function(otherItems) { 
    if (otherItems == "DONE") return; 
    //ELSE 
    self.otherItems = otherItems; 
    //Do further chaining 
}); 

因爲調用.then承諾的方法返回一個新的衍生承諾,很容易創建一系列的承諾。

,能夠創建任何長度和的鏈的許諾可與另一承諾(這將進一步推遲其分辨率)來解決,所以能夠暫停/在任何點推遲的承諾的決議鏈。這使得實現強大的API成爲可能。

— AngularJS $q Service API Reference - Chaining Promises

相關問題