我有一个新的userService工作正常,但我想仅在新用户启动网站时初始化某些变量。
例如,我只想获取用户的唯一会话ID一次,并从任何控制器访问该ID。我的理解是,我会在服务中初始化sessionID变量,然后每次调用服务时我都不需要再次进行http调用。
在这种情况下,app.js会满足我的目的吗?同样,我需要为每个控制器提供sessionID,而不需要每次都进行一次http调用(即以某种方式缓存sessionID)。
这是我当前的app.js,例如:
(function () {
'use strict';
var app = angular.module('app', [
// Angular modules
'ngAnimate',
'ngRoute',
'ngSanitize',
// Custom modules
'common', // common functions, logger, spinner
'common.bootstrap', // bootstrap dialog wrapper functions
// 3rd Party Modules
'ui.bootstrap', // ui-bootstrap (ex: carousel, pagination, dialog)
'kendo.directives', // Kendo UI
'app.customcontrollers' // Language/Currency settings
]);
app.run(['$route', function ($route) {
// Include $route to kick start the router.
}]);
})();
我的userService(this.openUserSession拉取sessionID):
(function () {
'use strict';
var app = angular.module('app');
app.service('userService', ['$http', 'common', userService] );
function userService($http, common){
// define private vars
var _rzEnvParams = [];
var _sessionID = '';
var _rzInitStatus = '';
var _domain = '';
var _port = '' ;
var _controllerpath = '';
var _space = '';
var _env = '';
var _clariteconfig = '';
var $q = common.$q;
this.initRz = function (rzEnvParams) {
// some lines of code omitted for brevity...
var url = ... ;
var deferred = $q.defer();
deferred.notify("Getting Rage init parameters...");
var retval = [];
$http({
method: 'GET',
encoding: 'JSON',
headers: {
'Access-Control-Allow-Origin': 'true'
},
withCredentials: true,
url: url
}).success(function (data, status, headers, config) {
retval = data;
deferred.resolve(retval);
});
return deferred.promise;
}
this.openUserSession = function(domain, port, controllerpath, user, pass) {
domain = "localhost:"
port = "49479";
controllerpath = "/api/open";
user = "";
pass = "";
var url = "http://" + domain + port + controllerpath + "?userid=" + user + "&pass=" + pass;
var deferred = $q.defer();
deferred.notify("Opening user session...");
var retval = [];
$http({
method: 'GET',
encoding: 'JSON',
headers: {
'Access-Control-Allow-Origin': 'true'
},
withCredentials: true,
url: url
}).success(function (data, status, headers, config) {
retval = data;
deferred.resolve(retval);
});
return deferred.promise;
}
}
})();
我目前从我的dashboard.js中调用openUserSession(),如下所示:
userService.openUserSession().then(function (data) {
response = data[0].split(",")
status = response[0];
vm.sessionID = response[1].split('"')[1];
});
但是我不需要以某种方式在userService中使用会话sessionID
,以便它在缓存中可用吗?
提前感谢。
鲍勃