Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module('myApp')
- .controller('AppCtrl', ['$scope', '$rootScope', '$location', '$filter', '$state', 'Navigation', 'AUTH_EVENTS', 'UserData', 'BASE_PATHS', 'validationMessages',
- 'DataApi', 'loadingPanel', 'pinesNotifications',
- function($scope, $rootScope, $location, $filter, $state, Navigation, AUTH_EVENTS, UserData, BASE_PATHS, validationMessages, DataApi, loadingPanel, pinesNotifications) {
- /* Global Vars */
- $scope.validationMessages = validationMessages;
- $scope.appBaseUrl = '';
- $scope.thisYear = new Date().getFullYear();
- $scope.app = {
- name: '',
- lastName: ' - ' + DataApi.brandName,
- }
- /* Global Helper Methods */
- $scope.isDataValida = function() { }
- $scope.isNumber = function(value) { }
- $scope.formatBytes = function(bytes, decimals) { }
- var handleLoginSuccess = function() {
- if(!$rootScope.UserData) {
- getUserData();
- } else {
- setCurrentUser();
- }
- }
- var getUserData = function() {
- var user = UserData.get('UserData');
- //The DataApi is a custom service to help me recover data from my api
- DataApi
- .get('users/' + user.ID)
- .then(function successCallback(result) {
- usuario.BusinessUnits = angular.fromJson(result.BusinessUnits);
- $rootScope.UserData = usuario;
- setCurrentUser();
- }, function errorCallback(result) {
- console.log(result);
- });
- }
- var setCurrentUser = function() {
- //Here i chose to save the user data on the rootScope to save the trips to the DB
- $scope.currentUser = $rootScope.UserData;
- //Here i do some processing with the user data and then call some other methods to get
- //some extra data used on all other controllers, those next methods also hit the database
- //to get their data and i can't afford that to happend on every state change
- $scope.loadProfessions();
- $scope.loadNotifications(true);
- }
- //Here i listen for the login success that will happen only once per page load
- //The event is fired inside my service that is responsible for validating the user
- $rootScope.$on(AUTH_EVENTS.loginSuccess, handleLoginSuccess);
- }
- ]);
- angular.module('myApp')
- .controller('HomeCtrl', ['$scope', 'DataApi', 'loadingPanel', 'pinesNotifications', '$filter', '$timeout', '$window', '$state',
- function($scope, DataApi, loadingPanel, pinesNotifications, $filter, $timeout, $window, $state) {
- $scope.loadHomeData = function() {}
- $scope.loadHomeData();
- }
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement