@@ -25,16 +25,17 @@ var isObject;
25
25
/* global -ngRouteModule */
26
26
var ngRouteModule = angular . module ( 'ngRoute' , [ 'ng' ] )
27
27
. provider ( '$route' , $RouteProvider )
28
- . factory ( '$$trackLocationChanges' , [ '$rootScope' , $$trackLocationChanges ] )
28
+ . factory ( '$$trackLocationChanges' , [ '$rootScope' , '$location' , $$trackLocationChanges ] )
29
29
. run ( [ '$$trackLocationChanges' , function ( $$trackLocationChanges ) {
30
30
$$trackLocationChanges . start ( ) ;
31
31
} ] ) ,
32
32
33
33
$routeMinErr = angular . $$minErr ( 'ngRoute' ) ;
34
34
35
- function $$trackLocationChanges ( $rootScope ) {
35
+ function $$trackLocationChanges ( $rootScope , $location ) {
36
36
var removeStartHandler , removeSuccessHandler ;
37
37
var service = {
38
+ events : [ ] ,
38
39
start : function ( ) {
39
40
removeStartHandler = $rootScope . $on ( '$locationChangeStart' , storeStartEvent ) ;
40
41
removeSuccessHandler = $rootScope . $on ( '$locationChangeSuccess' , storeSuccessEvent ) ;
@@ -47,13 +48,12 @@ function $$trackLocationChanges($rootScope) {
47
48
48
49
return service ;
49
50
50
- function storeStartEvent ( e ) {
51
- service . startEvent = e ;
52
- delete service . successEvent ;
51
+ function storeStartEvent ( e , url ) {
52
+ service . events . push ( { startEvent : e , locationPath : $location . path ( ) , locationSearch : $location . search ( ) } ) ;
53
53
}
54
54
55
55
function storeSuccessEvent ( e ) {
56
- service . successEvent = e ;
56
+ service . events [ service . events . length - 1 ] . successEvent = e ;
57
57
}
58
58
}
59
59
@@ -557,7 +557,7 @@ function $RouteProvider() {
557
557
} ;
558
558
559
559
$rootScope . $evalAsync ( function ( ) {
560
- prepareRoute ( fakeLocationEvent ) ;
560
+ prepareRoute ( fakeLocationEvent , $location . path ( ) , $location . search ( ) ) ;
561
561
if ( ! fakeLocationEvent . defaultPrevented ) commitRoute ( ) ;
562
562
} ) ;
563
563
} ,
@@ -587,13 +587,22 @@ function $RouteProvider() {
587
587
}
588
588
} ;
589
589
590
- if ( $$trackLocationChanges . successEvent ) {
591
- prepareRoute ( $$trackLocationChanges . startEvent ) ;
592
- commitRoute ( $$trackLocationChanges . successEvent ) ;
590
+ var eventPair ;
591
+ while ( eventPair = $$trackLocationChanges . events . pop ( ) ) {
592
+ // find the most recent success event
593
+ if ( eventPair . successEvent ) {
594
+ prepareRoute ( eventPair . startEvent , eventPair . locationPath , eventPair . locationSearch ) ;
595
+ // if the start event is not prevented then commit the change and escape
596
+ // otherwise try the previous location change
597
+ if ( ! eventPair . startEvent . defaultPrevented ) {
598
+ commitRoute ( eventPair . successEvent ) ;
599
+ break ;
600
+ }
601
+ }
593
602
}
594
603
$$trackLocationChanges . stop ( ) ;
595
604
596
- $rootScope . $on ( '$locationChangeStart' , prepareRoute ) ;
605
+ $rootScope . $on ( '$locationChangeStart' , function ( e ) { return prepareRoute ( e , $location . path ( ) , $location . search ( ) ) ; } ) ;
597
606
$rootScope . $on ( '$locationChangeSuccess' , commitRoute ) ;
598
607
599
608
return $route ;
@@ -632,10 +641,10 @@ function $RouteProvider() {
632
641
return params ;
633
642
}
634
643
635
- function prepareRoute ( $locationEvent ) {
644
+ function prepareRoute ( $locationEvent , locationPath , locationSearch ) {
636
645
var lastRoute = $route . current ;
637
646
638
- preparedRoute = parseRoute ( ) ;
647
+ preparedRoute = parseRoute ( locationPath , locationSearch ) ;
639
648
preparedRouteIsUpdateOnly = preparedRoute && lastRoute && preparedRoute . $$route === lastRoute . $$route
640
649
&& angular . equals ( preparedRoute . pathParams , lastRoute . pathParams )
641
650
&& ! preparedRoute . reloadOnSearch && ! forceReload ;
@@ -795,13 +804,13 @@ function $RouteProvider() {
795
804
/**
796
805
* @returns {Object } the current active route, by matching it against the URL
797
806
*/
798
- function parseRoute ( ) {
807
+ function parseRoute ( locationPath , locationSearch ) {
799
808
// Match a route
800
809
var params , match ;
801
810
angular . forEach ( routes , function ( route , path ) {
802
- if ( ! match && ( params = switchRouteMatcher ( $location . path ( ) , route ) ) ) {
811
+ if ( ! match && ( params = switchRouteMatcher ( locationPath , route ) ) ) {
803
812
match = inherit ( route , {
804
- params : angular . extend ( { } , $location . search ( ) , params ) ,
813
+ params : angular . extend ( { } , locationSearch , params ) ,
805
814
pathParams : params } ) ;
806
815
match . $$route = route ;
807
816
}
0 commit comments