I have a new userService that is working fine, but I would like to initialize certain vars only when a new user starts up the site.
For example, I would like ONLY fetch the user's unique session ID just once, and access that ID from any controller. My understanding is that I'll init the sessionID vars once in my service, then each time I call the service I will NOT need to make the http call again.
Would app.js serve my purposes in this case ? Again, I need to sessionID available to every controller WITHOUT the need to make an http call each time (i.e. caching the sessionID in some way).
Here's my current app.js for example :
(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.
}]);
})();
And my userService (this.openUserSession pulls the 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;
}
}
})();
I currently call openUserSession() from my dashboard.js as follows :
userService.openUserSession().then(function (data) {
response = data[0].split(",")
status = response[0];
vm.sessionID = response[1].split('"')[1];
});
but wouldn't I need to somehow session sessionID
within userService so it becomes available in cache ?
thanks in advance.
Bob