gpt4 book ai didi

javascript - 如何从 Controller 调用嵌套在 promise 内的函数?

转载 作者:行者123 更新时间:2023-11-28 06:40:36 24 4
gpt4 key购买 nike

这是我的工厂。我从工厂调用 promise ,因为我试图使 Controller 尽可能干净,并且 session 信息永远不需要更新/放入 $scope 中。但是,由于它是异步的,我如何从 Controller 调用 getNextEvent (另一个异步函数),并且仍然能够引用 var sessionId ?

(function(){
'use strict';

angular.module('csMgmtApp')
.factory('agentFactory', ['$http', function($http){

var result = {},
data = {},
access_token = result.access_token,
baseURI = result.resource_server_base_uri;

function startSession(startSessionPayload){
access_token = result.access_token;
baseURI = result.resource_server_base_uri;

return $http({
'url': baseURI + 'services/v6.0/agent-sessions',
'method': 'POST',
'headers': {'Authorization': 'bearer ' + access_token, 'content-Type': 'application/json'},
'data': startSessionPayload
}).then(function(res){
data.sessionId = res.data.sessionId;
console.log("sessionId", data.sessionId);


Want to call from controller --> function getNextEvent(timeout) {
$.ajax({
//The baseURI variable is created by the result.base_server_base_uri
//which is returned when getting a token and should be used to create the URL Base.
'url': baseURI + 'services/v6.0/agent-sessions/' + data.sessionId + '/get-next-event?timeout=' + timeout,
'type': 'GET',
'headers': {
//Use access_token previously retrieved from inContact token service
'Authorization': 'bearer ' + access_token,
'content-Type': 'application/x-www-form-urlencoded'
},
'success': function (result) {
//Process success actions
//Note the scenarios listed below are only a
});
});

return {startSession:startSession}
}]);
})();

这是 Controller :

csMgmtApp.controller('launchedController', ['$scope', '$http', '$document', '$resource', 'agentFactory', '$timeout', function ($scope, $http, $document, $resource, agentFactory, $timeout) {

$scope.agentStatePayload = {};
$scope.startSessionPayload = {
'stationPhoneNumber': '2222222222',
'inactivityTimeout': 0,
'inactivityForceLogout': 'false'
};

$document.ready(function () {
agentFactory.getToken();
});

agentFactory.startSession($scope.startSessionPayload);

}]);

最佳答案

Agent Factory 第 1 部分定义 startSession 函数

angular.module('csMgmtApp')
.factory('agentFactory', ['$http', function($http){
var baseURI = ""; //from controller
var accessToken = ""; //from controller
var sessionId = ""; //from startSession POST
var startResult = {}; //from startSession POST
var startData = {}; //from startSession POST

function startSession(startBaseURI, startAccessToken, startPayload){
//save baseURI and accessToken
baseURI = startBaseURI;
accessToken = startAccessToken;

var httpPromise = $http({
'url': baseURI + 'services/v6.0/agent-sessions',
'method': 'POST',
'headers': {'Authorization': 'bearer ' + accessToken,
'content-Type': 'application/json'},
'data': startPayload
});

var httpPromise2 = httpPromise.then( function(result) {
//save sessionId, result, data
sessionId = result.data.sessionId;
startResult = result;
startData = result.data;
//
return result; //always return something
});

return httpPromise2; //always return something
};

代理工厂第 2 部分定义 getNextEvent 函数

    function getNextEvent(timeout) {
var httpPromise = $http({
//use previously saved sessionID
'url': baseURI + 'services/v6.0/agent-sessions/' +
sessionId + '/get-next-event?timeout=' + timeout,
'method': 'POST',
'headers': {'Authorization': 'bearer ' + accessToken,
'content-Type': 'application/json'}
});
return httpPromise; //always return something
};

//always return something
return { "startSession": startSession,
"getNextEvent": getNextEvent
};
}]);

Controller 使用这些函数来链接 promise

angular.module('csMgmtApp')
.controller('launchedController', ['$scope','agentFactory',
function ($scope, agentFactory) {
var vm = $scope;
vm.baseURI = /* base URI here */ ;
vm.accessToken = /* access token here */ ;
vm.startPayload = {
'stationPhoneNumber': '2222222222',
'inactivityTimeout': 0,
'inactivityForceLogout': 'false'
};

//get functions from agentFactory
var startSession = agentFactory.startSession;
var getNextEvent = agentFactory.getNextEvent;

//Create start session promise
var startSessionPromise = startSession(vm.baseURI,
vm.accessToken,
vm.startPayload);

//Use startSessionPromise to create nextPromise
var nextPromise = startSessionPromise.then( function (result) {
vm.startData = result.data;
vm.startDataReady = true;
return getNextEvent(timeout); //call getNextEvent here
});

//Get data from nextPromise
nextPromise.then( function (result) {
vm.nextData = result.data;
vm.nextDataReady = true;
}) .catch ( function (error) {
//process errors
});
}]);

关于javascript - 如何从 Controller 调用嵌套在 promise 内的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33858562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com