Advertisement
Terkhos

myApp

Jan 9th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. angular.module('myApp')
  2.     .controller('AppCtrl', ['$scope', '$rootScope', '$location', '$filter', '$state', 'Navigation', 'AUTH_EVENTS', 'UserData', 'BASE_PATHS', 'validationMessages',
  3.                             'DataApi', 'loadingPanel', 'pinesNotifications',
  4.         function($scope, $rootScope, $location, $filter, $state, Navigation, AUTH_EVENTS, UserData, BASE_PATHS, validationMessages, DataApi, loadingPanel, pinesNotifications) {
  5.                 /* Global Vars */
  6.             $scope.validationMessages = validationMessages;
  7.             $scope.appBaseUrl = '';
  8.             $scope.thisYear = new Date().getFullYear();
  9.  
  10.             $scope.app = {
  11.                 name: '',
  12.                 lastName: ' - ' + DataApi.brandName,
  13.             }
  14.                        
  15.             /* Global Helper Methods */
  16.             $scope.isDataValida = function() { }
  17.             $scope.isNumber = function(value) { }
  18.             $scope.formatBytes = function(bytes, decimals) { }
  19.            
  20.             var handleLoginSuccess = function() {
  21.                 if(!$rootScope.UserData) {
  22.                     getUserData();
  23.                 } else {
  24.                     setCurrentUser();
  25.                 }
  26.             }
  27.  
  28.             var getUserData = function() {
  29.                 var user = UserData.get('UserData');
  30.                                
  31.                 //The DataApi is a custom service to help me recover data from my api
  32.                 DataApi
  33.                     .get('users/' + user.ID)
  34.                     .then(function successCallback(result) {
  35.  
  36.                         usuario.BusinessUnits = angular.fromJson(result.BusinessUnits);
  37.  
  38.                         $rootScope.UserData = usuario;
  39.  
  40.                         setCurrentUser();
  41.  
  42.                     }, function errorCallback(result) {
  43.                         console.log(result);
  44.                     });
  45.             }
  46.  
  47.             var setCurrentUser = function() {
  48.                     //Here i chose to save the user data on the rootScope to save the trips to the DB
  49.                 $scope.currentUser = $rootScope.UserData;
  50.  
  51.                //Here i do some processing with the user data and then call some other methods to get
  52.                //some extra data used on all other controllers, those next methods also hit the database
  53.                //to get their data and i can't afford that to happend on every state change
  54.  
  55.                 $scope.loadProfessions();
  56.                 $scope.loadNotifications(true);
  57.             }
  58.            
  59.             //Here i listen for the login success that will happen only once per page load
  60.             //The event is fired inside my service that is responsible for validating the user
  61.                         $rootScope.$on(AUTH_EVENTS.loginSuccess, handleLoginSuccess);
  62.         }
  63.     ]);
  64.    
  65. angular.module('myApp')
  66.     .controller('HomeCtrl', ['$scope', 'DataApi', 'loadingPanel', 'pinesNotifications', '$filter', '$timeout', '$window', '$state',
  67.         function($scope, DataApi, loadingPanel, pinesNotifications, $filter, $timeout, $window, $state) {
  68.          $scope.loadHomeData = function() {}
  69.          
  70.          $scope.loadHomeData();
  71.         }
  72.     ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement