0

我有一个控制器对两个 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;
            });
        }

错误是第一个调用没有按顺序解决,所以第二个没有输入。

是否有可能依次运行这两个调用以等待第二个调用来自第一个调用的输入?

非常感谢。

4

1 回答 1

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

于 2016-07-27T10:16:11.163 回答