如何调用嵌入在控制器的promise中的函数?

时间:2015-11-22 18:34:50

标签: javascript angularjs asynchronous promise angular-promise

这是我的工厂。我正在呼叫工厂的承诺,因为我正在努力保持我的控制器尽可能干净,并且会话信息永远不需要更新/放入$ 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);

}]);

1 个答案:

答案 0 :(得分:0)

一个选项可能是返回startSession的承诺,该承诺将使用sessionId解决。这将允许您从任何地方调用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,它可能会这样做,它仍然可以data访问sessionId虽然不是很确定......