Skip to content

Commit 97f8d90

Browse files
made .when defer till runtime
1 parent e756e08 commit 97f8d90

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/urlRouter.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
$UrlRouterProvider.$inject = ['$locationProvider', '$urlMatcherFactoryProvider'];
1818
function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
19-
var rules = [], otherwise = null, interceptDeferred = false, listener;
19+
var self = this, rules = [], otherwise = null, interceptDeferred = false, listener;
2020

2121
// Returns a string that is a prefix of all strings matching the RegExp
2222
function regExpPrefix(re) {
@@ -154,6 +154,20 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
154154
* @param {string|object} handler The path you want to redirect your user to.
155155
*/
156156
this.when = function (what, handler) {
157+
if (whenQueue)
158+
whenQueue.push({ what: what, handler: handler });
159+
else
160+
_when(what, handler);
161+
return self;
162+
};
163+
164+
var whenQueue = [];
165+
function flushWhenQueue() {
166+
forEach(whenQueue, function(queuedWhen) { _when(queuedWhen.what, queuedWhen.handler); });
167+
whenQueue = null;
168+
}
169+
170+
function _when(what, handler) {
157171
var redirect, handlerIsString = isString(handler);
158172
if (isString(what)) what = $urlMatcherFactory.compile(what);
159173

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

192206
for (var n in check) {
193-
if (check[n]) return this.rule(strategies[n](what, handler));
207+
if (check[n]) return self.rule(strategies[n](what, handler));
194208
}
195209

196210
throw new Error("invalid 'what' in when()");
197-
};
211+
}
198212

199213
/**
200214
* @ngdoc function
@@ -264,7 +278,7 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
264278
this.$get = $get;
265279
$get.$inject = ['$location', '$rootScope', '$injector', '$browser'];
266280
function $get( $location, $rootScope, $injector, $browser) {
267-
281+
flushWhenQueue();
268282
var baseHref = $browser.baseHref(), location = $location.url();
269283

270284
function appendBasePath(url, isHtml5, absolute) {

0 commit comments

Comments
 (0)