我有一个控制器针对两个REST服务进行两次$ resource调用,其中第一个的结果被第二个用作输入。
这里是代码:
if (requestLock == false) {
$scope.T_01_04_sharedData.tempRequestForT_01_04 = insertNewRequest("aggr_1", $rootScope.globals.currentUser.username, "", "temp", "2016-07-30 00:00:00");
requestLock = true;
}
if (action == 'add') {
updateSelectedForRequest(prosumer, 'selected', $rootScope.globals.currentUser.username, $scope.T_01_04_sharedData.tempRequestForT_01_04);
} else {
updateSelectedForRequest(prosumer, 'non-selected', $rootScope.globals.currentUser.username, $scope.T_01_04_sharedData.tempRequestForT_01_04);
}

功能updateSelectedForRequest
function updateSelectedForRequest(username, status, businessUser, request) {
WidgetService.T_01_04_updateSelectedForRequest.query({
businessUser_id: businessUser,
request_id: request,
username: username,
status: status
}, function (result) {
// response handler
});
}

功能insertNewRequest
function insertNewRequest(bu_id_target, requester, description, status, validUntil) {
return WidgetService.T_01_04_insertNewRequest.query({
bu_id_target: bu_id_target,
requester: requester,
description: description,
status: status,
validUntil: validUntil
}, function (result) {
$scope.T_01_04_sharedData.tempRequestForT_01_04 = result.request_id;
return result;
});
}

错误是第一个调用没有顺序解析,所以第二个调用没有输入。
是否有可能按顺序运行这两个调用以等待第二个调用第一个调用的输入?
非常感谢。
答案 0 :(得分:0)
我不熟悉ngresource,但你可以尝试这样的事情。
if (requestLock == false) {
insertNewRequest("aggr_1", $rootScope.globals.currentUser.username, "", "temp", "2016-07-30 00:00:00")
.then(function(result){
$scope.T_01_04_sharedData.tempRequestForT_01_04 = result;
if (action == 'add') {
updateSelectedForRequest(prosumer, 'selected', $rootScope.globals.currentUser.username, $scope.T_01_04_sharedData.tempRequestForT_01_04);
} else {
updateSelectedForRequest(prosumer, 'non-selected', $rootScope.globals.currentUser.username, $scope.T_01_04_sharedData.tempRequestForT_01_04);
}
}, function(error){/* manage error here */});
requestLock = true;
}
function insertNewRequest(bu_id_target, requester, description, status, validUntil) {
return new Promise(function(resolve, reject){
PromiseWidgetService.T_01_04_insertNewRequest.query({
bu_id_target: bu_id_target,
requester: requester,
description: description,
status: status,
validUntil: validUntil
}, function (result) {
$scope.T_01_04_sharedData.tempRequestForT_01_04 = result.request_id;
resolve(result);
});
})
}
有关承诺的更多信息,请访问:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise