Last active
December 3, 2015 07:01
-
-
Save 0x00dec0de/8de57f942c9a9393c2f0 to your computer and use it in GitHub Desktop.
cuts from angularjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$routeProvider.otherwise({ redirectTo: '/' }); | |
// Resolve Response | |
$provide.factory('HttpInterceptor', [ '$q', '$rootScope', '$location', function( $q, $rootScope, $location ) { | |
return { | |
request: function (config) { // On request success | |
return config || $q.when(config); // Return the config or wrap it in a promise if blank. | |
}, | |
requestError: function (rejection) { // On request failure | |
return $q.reject(rejection); // Return the promise rejection. | |
}, | |
response: function (response) { // On response success | |
return response || $q.when(response); // Return the response or promise. | |
}, | |
responseError: function (rejection) { // On response failture | |
if ( rejection.status === 401 ){ | |
$rootScope.ShowFlashMessage( rejection.data.flash, 'error' ); | |
$rootScope.session = false; | |
localStorage.removeItem('session'); | |
if ( $location.path() !== '/ServiceLogin.html' ) $location.path('/ServiceLogin.html'); | |
} | |
return $q.reject(rejection); // Return the promise rejection. | |
} | |
}; | |
}]); | |
$httpProvider.interceptors.push('HttpInterceptor'); // Add the interceptor to the $httpProvider. | |
}) // End Resolv Response | |
.service('apiService',['$http',function($http){ | |
var request = function( method, path, data ){ | |
return $http({ | |
method: method, | |
url: '/api/' + path + '.json', | |
data: data | |
}); | |
} | |
return { | |
get: function( path, data) { | |
return request( 'GET', path, data ); | |
}, | |
post: function( path, data) { | |
return request('POST', path, data ); | |
}, | |
put: function( path, data) { | |
return request( 'PUT' ,path, data ); | |
}, | |
delete: function( path, data) { | |
return request('DELETE', path, data ); | |
} | |
} | |
}]) | |
.directive('flashNotification', ['$animate', '$timeout', '$rootScope', function($animate, $timeout, $rootScope ) { | |
var isolateScope = $rootScope.$new(); | |
var flashNotification = { | |
restrict: 'E', | |
scope: {}, | |
link: function postLink(scope, element, attrs) { | |
isolateScope.$on('flashNotification::message', function(e, message, type ) { | |
if( type === undefined ) | |
type = "flash-notice"; | |
else | |
type = "flash-" + type ; | |
var messageElement = angular.element('<div class="flash-notification-message ' + type +'"></div>'); | |
messageElement.text(message); | |
element.append(messageElement); | |
var promise = $timeout( function() { $animate.leave( messageElement ) }, 4000); | |
messageElement.bind( 'mouseenter', function(e){ | |
$timeout.cancel(promise) | |
}); | |
messageElement.bind( 'mouseleave', function(e){ | |
promise = $timeout( function() { $animate.leave( messageElement ) }, 4000); | |
}); | |
messageElement.bind( 'click', function(e){ | |
messageElement.remove(); | |
}); | |
}); | |
} | |
}; | |
return flashNotification; | |
}]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/questions/20230691/injecting-state-ui-router-into-http-interceptor-causes-circular-dependency | |
// | |
// Jonathan's solution was great until I tried to save the current state. In ui-router v0.2.10 the current state does not seem to be populated on initial page load in the interceptor. | |
// Anyway, I solved it by using the $stateChangeError event instead. The $stateChangeError event gives you both to and from states, as well as the error. It's pretty nifty. | |
$rootScope.$on('$stateChangeError', | |
function(event, toState, toParams, fromState, fromParams, error){ | |
console.log('stateChangeError'); | |
console.log(toState, toParams, fromState, fromParams, error); | |
if(error.status == 401){ | |
console.log("401 detected. Redirecting..."); | |
authService.deniedState = toState.name; | |
$state.go("login"); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment