Skip to content

Commit

Permalink
made .when defer till runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Oct 21, 2014
1 parent e756e08 commit 97f8d90
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/urlRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
$UrlRouterProvider.$inject = ['$locationProvider', '$urlMatcherFactoryProvider'];
function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
var rules = [], otherwise = null, interceptDeferred = false, listener;
var self = this, rules = [], otherwise = null, interceptDeferred = false, listener;

// Returns a string that is a prefix of all strings matching the RegExp
function regExpPrefix(re) {
Expand Down Expand Up @@ -154,6 +154,20 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
* @param {string|object} handler The path you want to redirect your user to.
*/
this.when = function (what, handler) {
if (whenQueue)
whenQueue.push({ what: what, handler: handler });
else
_when(what, handler);
return self;
};

var whenQueue = [];
function flushWhenQueue() {
forEach(whenQueue, function(queuedWhen) { _when(queuedWhen.what, queuedWhen.handler); });
whenQueue = null;
}

function _when(what, handler) {
var redirect, handlerIsString = isString(handler);
if (isString(what)) what = $urlMatcherFactory.compile(what);

Expand Down Expand Up @@ -190,11 +204,11 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
var check = { matcher: $urlMatcherFactory.isMatcher(what), regex: what instanceof RegExp };

for (var n in check) {
if (check[n]) return this.rule(strategies[n](what, handler));
if (check[n]) return self.rule(strategies[n](what, handler));
}

throw new Error("invalid 'what' in when()");
};
}

/**
* @ngdoc function
Expand Down Expand Up @@ -264,7 +278,7 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
this.$get = $get;
$get.$inject = ['$location', '$rootScope', '$injector', '$browser'];
function $get( $location, $rootScope, $injector, $browser) {

flushWhenQueue();
var baseHref = $browser.baseHref(), location = $location.url();

function appendBasePath(url, isHtml5, absolute) {
Expand Down

0 comments on commit 97f8d90

Please sign in to comment.