[英]How to call a function nested within within a promise from the controller?
這是我的工廠。 我正在從工廠發出承諾,因為我試圖保持控制器盡可能整潔,並且會話信息永遠不需要真正更新/放入$ scope中。 但是,由於它是異步的,我將如何從控制器中調用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}
}]);
})();
這是控制器:
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);
}]);
一種選擇是從startSession
返回一個將由sessionId
解析的promise。 這將允許您從任何地方調用getNextevent
,並使主要客戶端知道當前會話。
//factory
function startSession(startSessionPayload) {
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);
// should return promise (based on bluebird.js), not very
// familiar with angular/jquery promises
return data.sessionId;
});
}
// Controller now gets sessionId and is responsible for calling next event
var sessionStartedPromise = agentFactory.startSession($scope.startSessionPayload);
sessionStartedPromise.then(function(sessionId) {
getNextEvent(timeout, sessionId);
});
上面的代碼將使getNextEvent
完全獨立於工廠,因為它將使用sessionId。 我不確定角度工廠的詳細信息以及它們如何在控制器之間加載,但這可能會使它如此,因此如果您導出getNextEvent
它仍然可以使用sessionId
訪問data
,盡管不確定...
代理工廠第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
};
}]);
控制器使用這些功能鏈接承諾
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
});
}]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.