@@ -419,6 +419,8 @@ function $RouteProvider(){
419
419
*/
420
420
421
421
var forceReload = false ,
422
+ preparedRoute ,
423
+ preparedRouteIsUpdateOnly ,
422
424
$route = {
423
425
routes : routes ,
424
426
@@ -435,7 +437,11 @@ function $RouteProvider(){
435
437
*/
436
438
reload : function ( ) {
437
439
forceReload = true ;
438
- $rootScope . $evalAsync ( updateRoute ) ;
440
+ $rootScope . $evalAsync ( function ( ) {
441
+ // Don't support cancellation of a reload for now...
442
+ prepareRoute ( ) ;
443
+ commitRoute ( ) ;
444
+ } ) ;
439
445
} ,
440
446
441
447
/**
@@ -469,7 +475,8 @@ function $RouteProvider(){
469
475
}
470
476
} ;
471
477
472
- $rootScope . $on ( '$locationChangeSuccess' , updateRoute ) ;
478
+ $rootScope . $on ( '$locationChangeStart' , prepareRoute ) ;
479
+ $rootScope . $on ( '$locationChangeSuccess' , commitRoute ) ;
473
480
474
481
return $route ;
475
482
@@ -507,54 +514,68 @@ function $RouteProvider(){
507
514
return params ;
508
515
}
509
516
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 ) {
517
+ function prepareRoute ( $locationEvent ) {
518
+ var lastRoute = $route . current ;
519
+
520
+ preparedRoute = parseRoute ( ) ;
521
+ preparedRouteIsUpdateOnly = preparedRoute && lastRoute && preparedRoute . $$route === lastRoute . $$route
522
+ && angular . equals ( preparedRoute . pathParams , lastRoute . pathParams )
523
+ && ! preparedRoute . reloadOnSearch && ! forceReload ;
524
+
525
+ if ( ! preparedRouteIsUpdateOnly && ( lastRoute || preparedRoute ) ) {
526
+ if ( $rootScope . $broadcast ( '$routeChangeStart' , preparedRoute , lastRoute ) . defaultPrevented ) {
527
+ if ( $locationEvent ) {
528
+ $locationEvent . preventDefault ( ) ;
529
+ }
530
+ }
531
+ }
532
+ }
533
+
534
+ function commitRoute ( ) {
535
+ var lastRoute = $route . current ;
536
+ var nextRoute = preparedRoute ;
537
+
538
+ if ( preparedRouteIsUpdateOnly ) {
539
+ lastRoute . params = nextRoute . params ;
540
+ angular . copy ( lastRoute . params , $routeParams ) ;
541
+ $rootScope . $broadcast ( '$routeUpdate' , lastRoute ) ;
542
+ } else if ( nextRoute || lastRoute ) {
521
543
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 )
544
+ $route . current = nextRoute ;
545
+ if ( nextRoute ) {
546
+ if ( nextRoute . redirectTo ) {
547
+ if ( angular . isString ( nextRoute . redirectTo ) ) {
548
+ $location . path ( interpolate ( nextRoute . redirectTo , nextRoute . params ) ) . search ( nextRoute . params )
528
549
. replace ( ) ;
529
550
} else {
530
- $location . url ( next . redirectTo ( next . pathParams , $location . path ( ) , $location . search ( ) ) )
551
+ $location . url ( nextRoute . redirectTo ( nextRoute . pathParams , $location . path ( ) , $location . search ( ) ) )
531
552
. replace ( ) ;
532
553
}
533
554
}
534
555
}
535
556
536
- $q . when ( next ) .
557
+ $q . when ( nextRoute ) .
537
558
then ( function ( ) {
538
- if ( next ) {
539
- var locals = angular . extend ( { } , next . resolve ) ,
559
+ if ( nextRoute ) {
560
+ var locals = angular . extend ( { } , nextRoute . resolve ) ,
540
561
template , templateUrl ;
541
562
542
563
angular . forEach ( locals , function ( value , key ) {
543
564
locals [ key ] = angular . isString ( value ) ?
544
565
$injector . get ( value ) : $injector . invoke ( value , null , null , key ) ;
545
566
} ) ;
546
567
547
- if ( angular . isDefined ( template = next . template ) ) {
568
+ if ( angular . isDefined ( template = nextRoute . template ) ) {
548
569
if ( angular . isFunction ( template ) ) {
549
- template = template ( next . params ) ;
570
+ template = template ( nextRoute . params ) ;
550
571
}
551
- } else if ( angular . isDefined ( templateUrl = next . templateUrl ) ) {
572
+ } else if ( angular . isDefined ( templateUrl = nextRoute . templateUrl ) ) {
552
573
if ( angular . isFunction ( templateUrl ) ) {
553
- templateUrl = templateUrl ( next . params ) ;
574
+ templateUrl = templateUrl ( nextRoute . params ) ;
554
575
}
555
576
templateUrl = $sce . getTrustedResourceUrl ( templateUrl ) ;
556
577
if ( angular . isDefined ( templateUrl ) ) {
557
- next . loadedTemplateUrl = templateUrl ;
578
+ nextRoute . loadedTemplateUrl = templateUrl ;
558
579
template = $templateRequest ( templateUrl ) ;
559
580
}
560
581
}
@@ -566,16 +587,16 @@ function $RouteProvider(){
566
587
} ) .
567
588
// after route change
568
589
then ( function ( locals ) {
569
- if ( next == $route . current ) {
570
- if ( next ) {
571
- next . locals = locals ;
572
- angular . copy ( next . params , $routeParams ) ;
590
+ if ( nextRoute == $route . current ) {
591
+ if ( nextRoute ) {
592
+ nextRoute . locals = locals ;
593
+ angular . copy ( nextRoute . params , $routeParams ) ;
573
594
}
574
- $rootScope . $broadcast ( '$routeChangeSuccess' , next , last ) ;
595
+ $rootScope . $broadcast ( '$routeChangeSuccess' , nextRoute , lastRoute ) ;
575
596
}
576
597
} , function ( error ) {
577
- if ( next == $route . current ) {
578
- $rootScope . $broadcast ( '$routeChangeError' , next , last , error ) ;
598
+ if ( nextRoute == $route . current ) {
599
+ $rootScope . $broadcast ( '$routeChangeError' , nextRoute , lastRoute , error ) ;
579
600
}
580
601
} ) ;
581
602
}
0 commit comments