File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed
Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -92,7 +92,8 @@ function $UrlRouterProvider( $urlMatcherFactory) {
9292 [ '$location' , '$rootScope' , '$injector' ,
9393 function ( $location , $rootScope , $injector ) {
9494 // TODO: Optimize groups of rules with non-empty prefix into some sort of decision tree
95- function update ( ) {
95+ function update ( evt ) {
96+ if ( evt && evt . defaultPrevented ) return ;
9697 function check ( rule ) {
9798 var handled = rule ( $injector , $location ) ;
9899 if ( handled ) {
@@ -110,7 +111,12 @@ function $UrlRouterProvider( $urlMatcherFactory) {
110111 }
111112
112113 $rootScope . $on ( '$locationChangeSuccess' , update ) ;
113- return { } ;
114+
115+ return {
116+ sync : function ( ) {
117+ update ( ) ;
118+ }
119+ } ;
114120 } ] ;
115121}
116122
Original file line number Diff line number Diff line change @@ -80,6 +80,32 @@ describe("UrlRouter", function () {
8080 expect ( custom . url . format ) . not . toHaveBeenCalled ( ) ;
8181 expect ( custom . handler ) . toHaveBeenCalled ( ) ;
8282 } ) ;
83+
84+ it ( 'can be cancelled by preventDefault() in $locationChangeSuccess' , inject ( function ( ) {
85+ var called ;
86+ location . path ( "/baz" ) ;
87+ scope . $on ( '$locationChangeSuccess' , function ( ev ) {
88+ ev . preventDefault ( ) ;
89+ called = true ;
90+ } ) ;
91+ scope . $emit ( "$locationChangeSuccess" ) ;
92+ expect ( called ) . toBeTruthy ( ) ;
93+ expect ( location . path ( ) ) . toBe ( "/baz" ) ;
94+ } ) ) ;
95+
96+ it ( 'can be deferred and updated in $locationChangeSuccess' , inject ( function ( $urlRouter , $timeout ) {
97+ var called ;
98+ location . path ( "/baz" ) ;
99+ scope . $on ( '$locationChangeSuccess' , function ( ev ) {
100+ ev . preventDefault ( ) ;
101+ called = true ;
102+ $timeout ( $urlRouter . sync , 2000 ) ;
103+ } ) ;
104+ scope . $emit ( "$locationChangeSuccess" ) ;
105+ $timeout . flush ( ) ;
106+ expect ( called ) . toBeTruthy ( ) ;
107+ expect ( location . path ( ) ) . toBe ( "/b4z" ) ;
108+ } ) ) ;
83109 } ) ;
84110
85111} ) ;
You can’t perform that action at this time.
0 commit comments