@@ -375,6 +375,10 @@ function $RouteProvider(){
375
375
* defined in `resolve` route property. Once all of the dependencies are resolved
376
376
* `$routeChangeSuccess` is fired.
377
377
*
378
+ * The route change (and the `$location` change that triggered it) can be prevented
379
+ * by calling `preventDefault` method of the event. See {@link ng.$rootScope.Scope#$on}
380
+ * for more details about event object.
381
+ *
378
382
* @param {Object } angularEvent Synthetic event object.
379
383
* @param {Route } next Future route information.
380
384
* @param {Route } current Current route information.
@@ -419,6 +423,8 @@ function $RouteProvider(){
419
423
*/
420
424
421
425
var forceReload = false ,
426
+ preparedRoute ,
427
+ preparedRouteIsUpdateOnly ,
422
428
$route = {
423
429
routes : routes ,
424
430
@@ -435,7 +441,11 @@ function $RouteProvider(){
435
441
*/
436
442
reload : function ( ) {
437
443
forceReload = true ;
438
- $rootScope . $evalAsync ( updateRoute ) ;
444
+ $rootScope . $evalAsync ( function ( ) {
445
+ // Don't support cancellation of a reload for now...
446
+ prepareRoute ( ) ;
447
+ commitRoute ( ) ;
448
+ } ) ;
439
449
} ,
440
450
441
451
/**
@@ -469,7 +479,8 @@ function $RouteProvider(){
469
479
}
470
480
} ;
471
481
472
- $rootScope . $on ( '$locationChangeSuccess' , updateRoute ) ;
482
+ $rootScope . $on ( '$locationChangeStart' , prepareRoute ) ;
483
+ $rootScope . $on ( '$locationChangeSuccess' , commitRoute ) ;
473
484
474
485
return $route ;
475
486
@@ -507,54 +518,68 @@ function $RouteProvider(){
507
518
return params ;
508
519
}
509
520
510
- function updateRoute ( ) {
511
- var next = parseRoute ( ) ,
512
- last = $route . current ;
513
-
514
- if ( next && last && next . $$route === last . $$route
515
- && angular . equals ( next . pathParams , last . pathParams )
516
- && ! next . reloadOnSearch && ! forceReload ) {
517
- last . params = next . params ;
518
- angular . copy ( last . params , $routeParams ) ;
519
- $rootScope . $broadcast ( '$routeUpdate' , last ) ;
520
- } else if ( next || last ) {
521
+ function prepareRoute ( $locationEvent ) {
522
+ var lastRoute = $route . current ;
523
+
524
+ preparedRoute = parseRoute ( ) ;
525
+ preparedRouteIsUpdateOnly = preparedRoute && lastRoute && preparedRoute . $$route === lastRoute . $$route
526
+ && angular . equals ( preparedRoute . pathParams , lastRoute . pathParams )
527
+ && ! preparedRoute . reloadOnSearch && ! forceReload ;
528
+
529
+ if ( ! preparedRouteIsUpdateOnly && ( lastRoute || preparedRoute ) ) {
530
+ if ( $rootScope . $broadcast ( '$routeChangeStart' , preparedRoute , lastRoute ) . defaultPrevented ) {
531
+ if ( $locationEvent ) {
532
+ $locationEvent . preventDefault ( ) ;
533
+ }
534
+ }
535
+ }
536
+ }
537
+
538
+ function commitRoute ( ) {
539
+ var lastRoute = $route . current ;
540
+ var nextRoute = preparedRoute ;
541
+
542
+ if ( preparedRouteIsUpdateOnly ) {
543
+ lastRoute . params = nextRoute . params ;
544
+ angular . copy ( lastRoute . params , $routeParams ) ;
545
+ $rootScope . $broadcast ( '$routeUpdate' , lastRoute ) ;
546
+ } else if ( nextRoute || lastRoute ) {
521
547
forceReload = false ;
522
- $rootScope . $broadcast ( '$routeChangeStart' , next , last ) ;
523
- $route . current = next ;
524
- if ( next ) {
525
- if ( next . redirectTo ) {
526
- if ( angular . isString ( next . redirectTo ) ) {
527
- $location . path ( interpolate ( next . redirectTo , next . params ) ) . search ( next . params )
548
+ $route . current = nextRoute ;
549
+ if ( nextRoute ) {
550
+ if ( nextRoute . redirectTo ) {
551
+ if ( angular . isString ( nextRoute . redirectTo ) ) {
552
+ $location . path ( interpolate ( nextRoute . redirectTo , nextRoute . params ) ) . search ( nextRoute . params )
528
553
. replace ( ) ;
529
554
} else {
530
- $location . url ( next . redirectTo ( next . pathParams , $location . path ( ) , $location . search ( ) ) )
555
+ $location . url ( nextRoute . redirectTo ( nextRoute . pathParams , $location . path ( ) , $location . search ( ) ) )
531
556
. replace ( ) ;
532
557
}
533
558
}
534
559
}
535
560
536
- $q . when ( next ) .
561
+ $q . when ( nextRoute ) .
537
562
then ( function ( ) {
538
- if ( next ) {
539
- var locals = angular . extend ( { } , next . resolve ) ,
563
+ if ( nextRoute ) {
564
+ var locals = angular . extend ( { } , nextRoute . resolve ) ,
540
565
template , templateUrl ;
541
566
542
567
angular . forEach ( locals , function ( value , key ) {
543
568
locals [ key ] = angular . isString ( value ) ?
544
569
$injector . get ( value ) : $injector . invoke ( value , null , null , key ) ;
545
570
} ) ;
546
571
547
- if ( angular . isDefined ( template = next . template ) ) {
572
+ if ( angular . isDefined ( template = nextRoute . template ) ) {
548
573
if ( angular . isFunction ( template ) ) {
549
- template = template ( next . params ) ;
574
+ template = template ( nextRoute . params ) ;
550
575
}
551
- } else if ( angular . isDefined ( templateUrl = next . templateUrl ) ) {
576
+ } else if ( angular . isDefined ( templateUrl = nextRoute . templateUrl ) ) {
552
577
if ( angular . isFunction ( templateUrl ) ) {
553
- templateUrl = templateUrl ( next . params ) ;
578
+ templateUrl = templateUrl ( nextRoute . params ) ;
554
579
}
555
580
templateUrl = $sce . getTrustedResourceUrl ( templateUrl ) ;
556
581
if ( angular . isDefined ( templateUrl ) ) {
557
- next . loadedTemplateUrl = templateUrl ;
582
+ nextRoute . loadedTemplateUrl = templateUrl ;
558
583
template = $templateRequest ( templateUrl ) ;
559
584
}
560
585
}
@@ -566,16 +591,16 @@ function $RouteProvider(){
566
591
} ) .
567
592
// after route change
568
593
then ( function ( locals ) {
569
- if ( next == $route . current ) {
570
- if ( next ) {
571
- next . locals = locals ;
572
- angular . copy ( next . params , $routeParams ) ;
594
+ if ( nextRoute == $route . current ) {
595
+ if ( nextRoute ) {
596
+ nextRoute . locals = locals ;
597
+ angular . copy ( nextRoute . params , $routeParams ) ;
573
598
}
574
- $rootScope . $broadcast ( '$routeChangeSuccess' , next , last ) ;
599
+ $rootScope . $broadcast ( '$routeChangeSuccess' , nextRoute , lastRoute ) ;
575
600
}
576
601
} , function ( error ) {
577
- if ( next == $route . current ) {
578
- $rootScope . $broadcast ( '$routeChangeError' , next , last , error ) ;
602
+ if ( nextRoute == $route . current ) {
603
+ $rootScope . $broadcast ( '$routeChangeError' , nextRoute , lastRoute , error ) ;
579
604
}
580
605
} ) ;
581
606
}
0 commit comments