File tree 2 files changed +26
-3
lines changed
2 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -551,13 +551,16 @@ angularWidget('ng:view', function(element) {
551
551
changeCounter ++ ;
552
552
} ) ;
553
553
554
- this . $watch ( function ( ) { return changeCounter ; } , function ( ) {
554
+ this . $watch ( function ( ) { return changeCounter ; } , function ( scope , newChangeCounter ) {
555
555
var template = $route . current && $route . current . template ;
556
556
if ( template ) {
557
557
//xhr's callback must be async, see commit history for more info
558
558
$xhr ( 'GET' , template , function ( code , response ) {
559
- element . html ( response ) ;
560
- compiler . compile ( element ) ( $route . current . scope ) ;
559
+ // ignore callback if another route change occured since
560
+ if ( newChangeCounter == changeCounter ) {
561
+ element . html ( response ) ;
562
+ compiler . compile ( element ) ( $route . current . scope ) ;
563
+ }
561
564
} ) ;
562
565
} else {
563
566
element . html ( '' ) ;
Original file line number Diff line number Diff line change @@ -539,6 +539,26 @@ describe("widget", function() {
539
539
540
540
expect ( rootScope . log ) . toEqual ( [ 'parent' , 'init' , 'child' ] ) ;
541
541
} ) ;
542
+
543
+ it ( 'should discard pending xhr callbacks if a new route is requested before the current ' +
544
+ 'finished loading' , function ( ) {
545
+ // this is a test for a bad race condition that affected feedback
546
+
547
+ $route . when ( '/foo' , { template : 'myUrl1' } ) ;
548
+ $route . when ( '/bar' , { template : 'myUrl2' } ) ;
549
+
550
+ expect ( rootScope . $element . text ( ) ) . toEqual ( '' ) ;
551
+
552
+ $location . path ( '/foo' ) ;
553
+ $browser . xhr . expectGET ( 'myUrl1' ) . respond ( '<div>{{1+3}}</div>' ) ;
554
+ rootScope . $digest ( ) ;
555
+ $location . path ( '/bar' ) ;
556
+ $browser . xhr . expectGET ( 'myUrl2' ) . respond ( '<div>{{1+1}}</div>' ) ;
557
+ rootScope . $digest ( ) ;
558
+ $browser . xhr . flush ( ) ; // no that we have to requests pending, flush!
559
+
560
+ expect ( rootScope . $element . text ( ) ) . toEqual ( '2' ) ;
561
+ } ) ;
542
562
} ) ;
543
563
544
564
You can’t perform that action at this time.
0 commit comments