AngularJS populating an object in a factory that is accessible throughout the website -


i have user factory has object called userproperties populate when user logs in system. function named login() called calls function named set() "sets user", or populates userproperties object.

user.factory.js

(function () {     'use strict';      angular         .module('app')         .factory('user', user);      /* @nginject */     function user($http, authtoken, $q, $state) {          var userproperties = {};          return {             login: login,             logout: logout,             set: set,             isset: isset,             get: get,             isloggedin: isloggedin         };          function login(email, password) {             return $http.post('/api/login', {                 email: email,                 password: password             }).then(function (response) {                 authtoken.settoken(response.data.jwt);                 set(response.data.user);                 return response;             });         }          function logout() {             authtoken.settoken();             $state.go('login');         }          function set(data) {             userproperties = data;         }          function get() {             return userproperties;         }                     function isset() {             return !isempty(userproperties) ? true : false;         }          function isloggedin() {             return authtoken.gettoken() ? true : false;         }          function isempty(obj) {             (var prop in obj) {                 if (obj.hasownproperty(prop)) { return false; }             }             return true;         }             }  }()); 

i able use code in controllers throughout website. example inside header controller, home controller, profile controller etc.

but if don't login manually, refresh site, have problem because not populating userproperties object did when logged in. need check when people click refresh button.

something similar code below. tried run code in run block before else in app, still did not appear in controllers because async.

if (user.isloggedin() && !user.isset()) {     $http.get('/api/user/0') // id: 0 id of logged in user         .then(function (response) {             user.set(response.data);         }); } 

but problem here if can't use user factory in controllers , access userproperties object got populated because async.

profile.controller.js

(function () {     'use strict';      angular         .module('app')         .controller('profile', profile);      /* @nginject */     function profile($scope, user) {         $scope.user = user.get();         console.log($scope.user);     }  }()); 

the code above profile controller outputs empty object console.

my alternative in ui-router, resolve before profile controller , set user here. have everywhere need user. in home controller, in header controller , on.

.state('profile', {     url: '/user/:id',     templateurl: 'app/profile/profile.tpl.html',     resolve: {         userresolve: function ($http, $q, user) {             return $http.get('/api/user/0')                 .then(function (response) {                     user.set(response.data);                 });         }     },     controller: 'profile' }); 

what should in order once , have available throughout website? or need resolve before every state?


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -