1

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

bob.mazzo
  • 5,183
  • 23
  • 80
  • 149
  • 1
    What about storing the session info in localStorage or a cookie? If you store it in a service it will not persist through hard page reloads. ngCookies is an internal angular service for this. You can then access the cookie from any other controller when you need it – user2936314 Aug 02 '14 at 00:11
  • @user2936314 - according to what I have read here, the data inside the service will indeed be persisted during the client session. See here http://stackoverflow.com/questions/23410997/how-to-set-a-value-to-a-global-variable-inside-a-function-in-javascript-angular – bob.mazzo Aug 04 '14 at 14:53
  • yes, but if you leave the page and then return it will kill the user session AFAIK. You may or may not want that behavior in your app – user2936314 Aug 04 '14 at 23:37

0 Answers0