16
16
*/
17
17
$UrlRouterProvider . $inject = [ '$locationProvider' , '$urlMatcherFactoryProvider' ] ;
18
18
function $UrlRouterProvider ( $locationProvider , $urlMatcherFactory ) {
19
- var rules = [ ] ,
20
- otherwise = null ;
19
+ var rules = [ ] , otherwise = null , interceptDeferred = false , listener ;
21
20
22
21
// Returns a string that is a prefix of all strings matching the RegExp
23
22
function regExpPrefix ( re ) {
@@ -38,7 +37,7 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
38
37
* @methodOf ui.router.router.$urlRouterProvider
39
38
*
40
39
* @description
41
- * Defines rules that are used by `$urlRouterProvider to find matches for
40
+ * Defines rules that are used by `$urlRouterProvider` to find matches for
42
41
* specific URLs.
43
42
*
44
43
* @example
@@ -61,7 +60,7 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
61
60
* @param {object } rule Handler function that takes `$injector` and `$location`
62
61
* services as arguments. You can use them to return a valid path as a string.
63
62
*
64
- * @return {object } $urlRouterProvider - $urlRouterProvider instance
63
+ * @return {object } ` $urlRouterProvider` - ` $urlRouterProvider` instance
65
64
*/
66
65
this . rule = function ( rule ) {
67
66
if ( ! isFunction ( rule ) ) throw new Error ( "'rule' must be a function" ) ;
@@ -75,7 +74,7 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
75
74
* @methodOf ui.router.router.$urlRouterProvider
76
75
*
77
76
* @description
78
- * Defines a path that is used when an invalied route is requested.
77
+ * Defines a path that is used when an invalid route is requested.
79
78
*
80
79
* @example
81
80
* <pre>
@@ -98,7 +97,7 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
98
97
* rule that returns the url path. The function version is passed two params:
99
98
* `$injector` and `$location` services.
100
99
*
101
- * @return {object } $urlRouterProvider - $urlRouterProvider instance
100
+ * @return {object } ` $urlRouterProvider` - ` $urlRouterProvider` instance
102
101
*/
103
102
this . otherwise = function ( rule ) {
104
103
if ( isString ( rule ) ) {
@@ -124,8 +123,8 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
124
123
*
125
124
* @description
126
125
* Registers a handler for a given url matching. if handle is a string, it is
127
- * treated as a redirect, and is interpolated according to the syyntax of match
128
- * (i.e. like String.replace() for RegExp, or like a UrlMatcher pattern otherwise).
126
+ * treated as a redirect, and is interpolated according to the syntax of match
127
+ * (i.e. like ` String.replace()` for ` RegExp` , or like a ` UrlMatcher` pattern otherwise).
129
128
*
130
129
* If the handler is a function, it is injectable. It gets invoked if `$location`
131
130
* matches. You have the option of inject the match object as `$match`.
@@ -197,6 +196,59 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
197
196
throw new Error ( "invalid 'what' in when()" ) ;
198
197
} ;
199
198
199
+ /**
200
+ * @ngdoc function
201
+ * @name ui.router.router.$urlRouterProvider#deferIntercept
202
+ * @methodOf ui.router.router.$urlRouterProvider
203
+ *
204
+ * @description
205
+ * Disables (or enables) deferring location change interception.
206
+ *
207
+ * If you wish to customize the behavior of syncing the URL (for example, if you wish to
208
+ * defer a transition but maintain the current URL), call this method at configuration time.
209
+ * Then, at run time, call `$urlRouter.listen()` after you have configured your own
210
+ * `$locationChangeSuccess` event handler.
211
+ *
212
+ * @example
213
+ * <pre>
214
+ * var app = angular.module('app', ['ui.router.router']);
215
+ *
216
+ * app.config(function ($urlRouterProvider) {
217
+ *
218
+ * // Prevent $urlRouter from automatically intercepting URL changes;
219
+ * // this allows you to configure custom behavior in between
220
+ * // location changes and route synchronization:
221
+ * $urlRouterProvider.deferIntercept();
222
+ *
223
+ * }).run(function ($rootScope, $urlRouter, UserService) {
224
+ *
225
+ * $rootScope.$on('$locationChangeSuccess', function(e) {
226
+ * // UserService is an example service for managing user state
227
+ * if (UserService.isLoggedIn()) return;
228
+ *
229
+ * // Prevent $urlRouter's default handler from firing
230
+ * e.preventDefault();
231
+ *
232
+ * UserService.handleLogin().then(function() {
233
+ * // Once the user has logged in, sync the current URL
234
+ * // to the router:
235
+ * $urlRouter.sync();
236
+ * });
237
+ * });
238
+ *
239
+ * // Configures $urlRouter's listener *after* your custom listener
240
+ * $urlRouter.listen();
241
+ * });
242
+ * </pre>
243
+ *
244
+ * @param {boolean } defer Indicates whether to defer location change interception. Passing
245
+ no parameter is equivalent to `true`.
246
+ */
247
+ this . deferIntercept = function ( defer ) {
248
+ if ( defer === undefined ) defer = true ;
249
+ interceptDeferred = defer ;
250
+ }
251
+
200
252
/**
201
253
* @ngdoc object
202
254
* @name ui.router.router.$urlRouter
@@ -242,7 +294,12 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
242
294
if ( otherwise ) check ( otherwise ) ;
243
295
}
244
296
245
- $rootScope . $on ( '$locationChangeSuccess' , update ) ;
297
+ function listen ( ) {
298
+ listener = listener || $rootScope . $on ( '$locationChangeSuccess' , update ) ;
299
+ return listener ;
300
+ }
301
+
302
+ if ( ! interceptDeferred ) listen ( ) ;
246
303
247
304
return {
248
305
/**
@@ -275,6 +332,10 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
275
332
update ( ) ;
276
333
} ,
277
334
335
+ listen : function ( ) {
336
+ return listen ( ) ;
337
+ } ,
338
+
278
339
update : function ( read ) {
279
340
if ( read ) {
280
341
location = $location . url ( ) ;
0 commit comments