diff options
| author | Rado Kirov | 2012-09-21 18:57:22 -0700 | 
|---|---|---|
| committer | Igor Minar | 2012-11-26 23:24:39 +0100 | 
| commit | fc781560a341c804b02c7efd30c6d3010206401c (patch) | |
| tree | 48de9757da5982e49eab1d79b58bd985a3726a27 | |
| parent | c9199ee6634af6b960524d91bdae706860a49d0c (diff) | |
| download | angular.js-fc781560a341c804b02c7efd30c6d3010206401c.tar.bz2 | |
fix($location): reset $location.$$replace with every watch call
Closes #1111
| -rw-r--r-- | src/ng/location.js | 5 | ||||
| -rw-r--r-- | test/ng/locationSpec.js | 23 | ||||
| -rw-r--r-- | test/ng/routeSpec.js | 13 | 
3 files changed, 30 insertions, 11 deletions
| diff --git a/src/ng/location.js b/src/ng/location.js index cf50952d..73ef7f7b 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -590,6 +590,7 @@ function $LocationProvider(){      var changeCounter = 0;      $rootScope.$watch(function $locationWatch() {        var oldUrl = $browser.url(); +      var currentReplace = $location.$$replace;        if (!changeCounter || oldUrl != $location.absUrl()) {          changeCounter++; @@ -598,12 +599,12 @@ function $LocationProvider(){                defaultPrevented) {              $location.$$parse(oldUrl);            } else { -            $browser.url($location.absUrl(), $location.$$replace); -            $location.$$replace = false; +            $browser.url($location.absUrl(), currentReplace);              afterLocationChange(oldUrl);            }          });        } +      $location.$$replace = false;        return changeCounter;      }); diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 17ae13f3..cb3372a3 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -447,6 +447,29 @@ describe('$location', function() {        expect($browserUrl).toHaveBeenCalledOnce();        expect($browserUrl.mostRecentCall.args).toEqual(['http://new.com/a/b#!/n/url', true]); +      expect($location.$$replace).toBe(false); +    })); + + +    it('should always reset replace flag after running watch', inject(function($rootScope, $location) { +      // init watches +      $location.url('/initUrl'); +      $rootScope.$apply(); + +      // changes url but resets it before digest +      $location.url('/newUrl').replace().url('/initUrl'); +      $rootScope.$apply(); +      expect($location.$$replace).toBe(false); + +      // set the url to the old value +      $location.url('/newUrl').replace(); +      $rootScope.$apply(); +      expect($location.$$replace).toBe(false); + +      // doesn't even change url only calls replace() +      $location.replace(); +      $rootScope.$apply(); +      expect($location.$$replace).toBe(false);      })); diff --git a/test/ng/routeSpec.js b/test/ng/routeSpec.js index 0e98be5f..52fffa40 100644 --- a/test/ng/routeSpec.js +++ b/test/ng/routeSpec.js @@ -560,20 +560,15 @@ describe('$route', function() {          $routeProvider.when('/bar/:id', {templateUrl: 'bar.html'});          $routeProvider.when('/foo/:id/:extra', {redirectTo: '/bar/:id'});        }); -      inject(function($route, $location, $rootScope) { -        var replace; - -        $rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) { -          if (oldUrl == 'http://server/#/foo/id3/eId') { -            replace = $location.$$replace; -          } -        }); +      inject(function($browser, $route, $location, $rootScope) { +        var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();          $location.path('/foo/id3/eId');          $rootScope.$digest();          expect($location.path()).toEqual('/bar/id3'); -        expect(replace).toBe(true); +        expect($browserUrl.mostRecentCall.args) +            .toEqual(['http://server/#/bar/id3?extra=eId', true]);        });      });    }); | 
