Last active
August 29, 2015 13:57
-
-
Save ada-lovecraft/9414111 to your computer and use it in GitHub Desktop.
$modal decorator to close all modals on $rootScope noauth message
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
torpedoApp.config(function($provide) { | |
$provide.decorator('$modal', function($delegate,$rootScope) { | |
var openFn = $delegate.open; | |
$delegate.open = function() { | |
console.debug('open!'); | |
var regged = false; | |
var args = [].slice.call(arguments); | |
var modalInstance = openFn.apply(null, args); | |
modalInstance.opened.then(function(status) { | |
console.debug('open status:', status); | |
if(!!status && !regged) { | |
regged = true; | |
console.debug('registering for no auth'); | |
var noAuthDereg = $rootScope.$on('noauth', function() { | |
console.debug('noauth!', modalInstance); | |
noAuthDereg(); | |
modalInstance.dismiss('noauth'); | |
}); | |
} | |
}); | |
return modalInstance; | |
}; | |
return $delegate; | |
}); | |
}); | |
// and then inside the http interceptor: | |
if(rejection.status === 401) { | |
// if the user is no longer authorized, push them to the login page | |
console.debug('No longer authorized. Transitioning to login'); | |
$rootScope.$broadcast('noauth'); | |
$injector.get('$state').transitionTo('noauth.login'); | |
} | |
return $q.reject(rejection); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment