我正在开发一个实用程序服务,用于处理角度的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");
});
};
答案 0 :(得分:0)
我认为@Bergi建议我做以下事情:
this.GetListItemById = function (webUrl, listName, itemId, loading) {
var url = webUrl + "/_vti_bin/listdata.svc/" + listName + "(" + itemId + ")";
return $http({
url: url,
method: 'GET',
processData: false,
headers: { "Accept": "application/json;odata=verbose" },
}).success(function (data) {
console.log("This Executes");
return data.d;
}).error( function (response) {
return response;
});
};
这似乎有点像我想要的那样。它执行异步调用,但它始终返回包含data,status,headers,config和statusText的整个success / then对象。即使我特意说返回数据。它仍然返回整个对象。谁知道我错过了什么?
答案 1 :(得分:0)
感谢@Bergi的回复。如果请求失败,我想将响应返回给用户。这是我的控制器中的代码:
$scope.GetListItemById = function (webUrl, listName, itemId) {
SPListUtility.GetListItemById(webUrl, listName, itemId)
.then(function success(data) {
console.log(JSON.stringify(data));
$scope.animateAlert("myAlert", "alertText", "Item: " + data.Title + " loaded", "success");
}).catch( function failure(error) {
console.log("Error " + error.status + ": " + error.statusText);
$scope.animateAlert("myAlert", "alertText", "Error " + error.status + ": " + error.statusText, "danger");
});
};
现在这些主要用于实验和测试,但我希望这项服务能够在未来发挥作用。然后似乎适用于存在的项目,但问题是,如果我尝试检索一个不存在的项目并且它抛出404,那么上面列出的那个函数在它返回时仍会被调用而不是像我一样的catch打算。我可以通过手动返回和Promise.reject更改服务功能来强制它工作,但是遵循最佳实践吗?
this.GetListItemById = function (webUrl, listName, itemId, loading) {
var url = webUrl + "/_vti_bin/listdata.svc/" + listName + "(" + itemId + ")";
//var deferred = $q.defer();
return $http({
url: url,
method: 'GET',
processData: false,
headers: { "Accept": "application/json;odata=verbose" },
}).then(function SuccessCB(response) {
//console.log(JSON.stringify(response));
return response.data.d;
}, function FailureCB(response) {
return Promise.reject(response);
});
//return deferred.promise;
};