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) {
92
92
[ '$location' , '$rootScope' , '$injector' ,
93
93
function ( $location , $rootScope , $injector ) {
94
94
// 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 ;
96
97
function check ( rule ) {
97
98
var handled = rule ( $injector , $location ) ;
98
99
if ( handled ) {
@@ -110,7 +111,12 @@ function $UrlRouterProvider( $urlMatcherFactory) {
110
111
}
111
112
112
113
$rootScope . $on ( '$locationChangeSuccess' , update ) ;
113
- return { } ;
114
+
115
+ return {
116
+ sync : function ( ) {
117
+ update ( ) ;
118
+ }
119
+ } ;
114
120
} ] ;
115
121
}
116
122
Original file line number Diff line number Diff line change @@ -80,6 +80,32 @@ describe("UrlRouter", function () {
80
80
expect ( custom . url . format ) . not . toHaveBeenCalled ( ) ;
81
81
expect ( custom . handler ) . toHaveBeenCalled ( ) ;
82
82
} ) ;
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
+ } ) ) ;
83
109
} ) ;
84
110
85
111
} ) ;
You can’t perform that action at this time.
0 commit comments