Skip to content

Instantly share code, notes, and snippets.

@10thfloor
Last active November 9, 2016 00:28
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 10thfloor/1f6652d644f965aa54d9 to your computer and use it in GitHub Desktop.
Save 10thfloor/1f6652d644f965aa54d9 to your computer and use it in GitHub Desktop.
$routeProvider.when = (function(when){
var extendedWhen = function(incomingRoute, routeConfig){
// Basic error case.
if(routeConfig.resolve && typeof routeConfig.resolve !== 'object'){
throw new Error('$routeProvider.when: resolve should be \'object\' instead of: ' + typeof config.resolve );
}
routeConfig = routeConfig || {};
routeConfig.resolve = angular.extend(routeConfig.resolve, {
CHECK_FOR_LOGGED_IN_USER : ['waitForAuth','$rootScope', '$q', 'routingHelpers',
function(waitForAuth, $rootScope, $q, routingHelpers){
var deferred = $q.defer();
waitForAuth.then(function() {
if(routingHelpers.shouldRedirect(incomingRoute)){
deferred.reject({'REDIRECT_ROUTE' : { attemptedRoute: incomingRoute, to: '/' }});
}else{
deferred.resolve();
}
});
return deferred.promise;
}]
});
return when.apply(this, [incomingRoute, routeConfig]);
};
return extendedWhen;
})($routeProvider.when);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment