Skip to content

Instantly share code, notes, and snippets.

@joncodo
Created November 26, 2014 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joncodo/962a2c2a03be9970eaf9 to your computer and use it in GitHub Desktop.
Save joncodo/962a2c2a03be9970eaf9 to your computer and use it in GitHub Desktop.
myApp.controller('navController', function ($scope, $rootScope, $http, $location, UtilService) {
$scope.userResults = [];
$scope.searchPerformed = false;
$scope.searchToggled = false;
if ( !$rootScope.currentUser && UtilService.retrieve('x-access-token') ) {
console.log('Route changed and there is no current user set. Also, there now IS a token. Getting current user...');
$http.get('/me')
.success(function(data, status, headers, config) {
$rootScope.currentUser = {
first_name: data.first_name,
last_name: data.last_name,
user_type: data.userType,
is_admin: data.is_admin
};
console.log('Current user set!');
});
}
$rootScope.$on('$locationChangeSuccess', function(event, current){
$rootScope.resetActiveNav();
if ( current.indexOf('/dashboard/') > -1 ) {
$rootScope.navView.dashboardActive = true;
} else if ( current.indexOf('/profile/') > -1 ) {
$rootScope.navView.profileActive = true;
} else if ( current.indexOf('/users/browse/') > -1 ) {
$rootScope.navView.usersActive = true;
} else if ( current.indexOf('/reports/') > -1 ) {
$rootScope.navView.reportsActive = true;
}
});
$scope.performSearch = function(event) {
$scope.searchQuery = 'foo';
var query = $scope.searchQuery;
console.log('query', $scope.searchQuery);
console.log('in the perform search block');
if ( query.length < 1 ) {
$scope.searchPerformed = false;
} else if ( event.which === 13 && query.length > 0 ) {
console.log('Enter pressed while search was active!');
$http.post('/search', { query: query })
.success(function(data, status, headers, config){
console.log('Success!');
console.log('data', data);
$scope.userResults = data;
$scope.searchPerformed = true;
})
.error(function(data, status, headers, config){
$scope.userResults = [];
$scope.searchPerformed = true;
console.log('An error occured searching!');
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment