简体   繁体   English

依次调用ngresource

[英]Sequentially ngresource calls

I have a controller making two $resource calls against two REST services where the result of the first one is used as input by the second one. 我有一个控制器对两个REST服务进行两个$ resource调用,其中第一个服务的结果用作第二个服务的输入。

Here the code: 这里的代码:

 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); } 

Function updateSelectedForRequest 函数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 }); } 

Function insertNewRequest 函数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; }); } 

The error is that the first call is not resolved sequentially so the second one has no input. 错误是第一个调用没有顺序解决,因此第二个没有输入。

Is there the possibility to run these two calls sequentially in order to wait for the second call the input from the first one? 是否有可能依次运行这两个调用,以等待第二个调用从第一个调用的输入?

Thanks a lot. 非常感谢。

I'm not familiar with ngresource, but you can try something like this. 我对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); }); }) } 

more information on promise here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise 有关诺言的更多信息,请访问: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM