我正在開發一個實用程序服務,用於處理角度中的SharePoint項目。我正在更新我的代碼以對異步使用角度承諾。而不是回調函數,所以請原諒我的代碼在轉換過程中有點混亂。我已經寫了一些代碼來更新單個列表項目,然後我重複使用該函數來批量更新多個項目。該代碼正在工作,http請求正在改變我的列表中的項目,但我似乎無法獲得在更新多個項目時回到頂端的承諾。這裏是我的代碼:
this.UpdateListItem = function (webUrl, listName, itemId, itemProperties, success, failure) {
if (typeof lists[listName] === 'undefined') {
lists[listName] = [];
}
var deferred = $q.defer();
var post = angular.copy(itemProperties);
DataUtilitySvc.ConvertDatesJson(post);
this.GetListItemById(webUrl, listName, itemId)
.then(function (item) {
$http({
url: item.__metadata.uri,
method: 'POST',
contentType: 'application/json',
processData: false,
headers: {
"Accept": "application/json;odata=verbose",
"X-HTTP-Method": "MERGE",
"If-Match": item.__metadata.etag
},
data: JSON.stringify(post),
dataType: "json",
}).then(function SuccessCB(response) {
var temp = [];
temp.push(itemProperties);
DataUtilitySvc.MergeByProperty(lists[listName], temp, 'Id');
deferred.resolve(response);
}, function FailureCB(response) {
this.GetListItems(webUrl, listName);
deferred.reject(response);
});
}, function (error) {
deferred.reject(error);
});
return deferred.promise;
};
this.UpdateListItems = function (webUrl, listName, itemsJson, success, failure) {
if (numItems == -1) {
numItems = itemsJson.length;
c = 0;
f = 0;
}
var promises = [];
itemsJson.forEach(function (itemProps) {
var deferred = $q.defer();
this.UpdateListItem(webUrl, listName, itemProps.Id, itemProps)
.then(function() {
c++;
if (c == numItems && f == 0) {
numItems = -1;
deferred.resolve(itemsJson[listName]);
}
}, function (error) {
c++; f++;
if (c == numItems) {
numItems = -1;
deferred.reject(error);
}
});
promises.push(deferred.promise);
}, this);
return $q.all(promises);
};
然後這裏是我從哪裏調用角度控制器的服務功能。 animateAlert調用會使引導警報出現,然後用指定的文本消失。
$scope.UpdateListItem = function (webUrl, listName, itemId, itemProperties, success, failure) {
SPListUtility.UpdateListItem(webUrl, listName, itemId, itemProperties, success, failure)
// The following then clause works and the animations show as intended
.then(function success(data) {
console.log(JSON.stringify(data));
$scope.animateAlert("myAlert", "alertText", "Operation Successful!", "success");
}, function failure(error) {
console.log("Error " + error.status + ": " + error.statusText);
$scope.animateAlert("myAlert", "alertText", "Error " + error.status + ": " + error.statusText, "danger");
});
};
$scope.UpdateListItems = function (webUrl, listName, itemsJson, success, failure) {
SPListUtility.UpdateListItems(webUrl, listName, itemsJson, success, failure)
// The following section is what never seems to get called. These animations never show up
.then(function success() {
$scope.animateAlert("myAlert", "alertText", "Items Saved", "success");
}, function failure(error) {
console.log("Error " + error.status + ": " + error.statusText);
$scope.animateAlert("myAlert", "alertText", "Error " + error.status + ": " + error.statusText, "danger");
});
};
只有在'(c == numItems && f == 0)'爲真時,您的承諾纔會被解決。否則,他們永遠不會解決也不會被拒絕。 –
避免[deferred antipattern](http://stackoverflow.com/q/23803743/1048572)!你可以通過[簡單地''將它們從'then'回調](http://stackoverflow.com/a/22562045/1048572)返回到頂端「來回報頂部。 – Bergi
嗨@Bergi,你能否改變我的一些代碼來證明你的意思。我閱讀了您發佈的文章,但我無法弄清楚如何將其應用於我的情況。謝謝。 –