|
16 | 16 | */ |
17 | 17 | $UrlRouterProvider.$inject = ['$locationProvider', '$urlMatcherFactoryProvider']; |
18 | 18 | function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) { |
19 | | - var rules = [], otherwise = null, interceptDeferred = false, listener; |
| 19 | + var self = this, rules = [], otherwise = null, interceptDeferred = false, listener; |
20 | 20 |
|
21 | 21 | // Returns a string that is a prefix of all strings matching the RegExp |
22 | 22 | function regExpPrefix(re) { |
@@ -154,6 +154,20 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) { |
154 | 154 | * @param {string|object} handler The path you want to redirect your user to. |
155 | 155 | */ |
156 | 156 | 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) { |
157 | 171 | var redirect, handlerIsString = isString(handler); |
158 | 172 | if (isString(what)) what = $urlMatcherFactory.compile(what); |
159 | 173 |
|
@@ -190,11 +204,11 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) { |
190 | 204 | var check = { matcher: $urlMatcherFactory.isMatcher(what), regex: what instanceof RegExp }; |
191 | 205 |
|
192 | 206 | 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)); |
194 | 208 | } |
195 | 209 |
|
196 | 210 | throw new Error("invalid 'what' in when()"); |
197 | | - }; |
| 211 | + } |
198 | 212 |
|
199 | 213 | /** |
200 | 214 | * @ngdoc function |
@@ -264,7 +278,7 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) { |
264 | 278 | this.$get = $get; |
265 | 279 | $get.$inject = ['$location', '$rootScope', '$injector', '$browser']; |
266 | 280 | function $get( $location, $rootScope, $injector, $browser) { |
267 | | - |
| 281 | + flushWhenQueue(); |
268 | 282 | var baseHref = $browser.baseHref(), location = $location.url(); |
269 | 283 |
|
270 | 284 | function appendBasePath(url, isHtml5, absolute) { |
|
0 commit comments