fix($location): reset $location.$$replace with every watch call

Closes #1111
This commit is contained in:
Rado Kirov 2012-09-21 18:57:22 -07:00 committed by Igor Minar
parent c9199ee663
commit fc781560a3
3 changed files with 30 additions and 11 deletions

View file

@ -590,6 +590,7 @@ function $LocationProvider(){
var changeCounter = 0; var changeCounter = 0;
$rootScope.$watch(function $locationWatch() { $rootScope.$watch(function $locationWatch() {
var oldUrl = $browser.url(); var oldUrl = $browser.url();
var currentReplace = $location.$$replace;
if (!changeCounter || oldUrl != $location.absUrl()) { if (!changeCounter || oldUrl != $location.absUrl()) {
changeCounter++; changeCounter++;
@ -598,12 +599,12 @@ function $LocationProvider(){
defaultPrevented) { defaultPrevented) {
$location.$$parse(oldUrl); $location.$$parse(oldUrl);
} else { } else {
$browser.url($location.absUrl(), $location.$$replace); $browser.url($location.absUrl(), currentReplace);
$location.$$replace = false;
afterLocationChange(oldUrl); afterLocationChange(oldUrl);
} }
}); });
} }
$location.$$replace = false;
return changeCounter; return changeCounter;
}); });

View file

@ -447,6 +447,29 @@ describe('$location', function() {
expect($browserUrl).toHaveBeenCalledOnce(); expect($browserUrl).toHaveBeenCalledOnce();
expect($browserUrl.mostRecentCall.args).toEqual(['http://new.com/a/b#!/n/url', true]); 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);
})); }));

View file

@ -560,20 +560,15 @@ describe('$route', function() {
$routeProvider.when('/bar/:id', {templateUrl: 'bar.html'}); $routeProvider.when('/bar/:id', {templateUrl: 'bar.html'});
$routeProvider.when('/foo/:id/:extra', {redirectTo: '/bar/:id'}); $routeProvider.when('/foo/:id/:extra', {redirectTo: '/bar/:id'});
}); });
inject(function($route, $location, $rootScope) { inject(function($browser, $route, $location, $rootScope) {
var replace; var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();
$rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) {
if (oldUrl == 'http://server/#/foo/id3/eId') {
replace = $location.$$replace;
}
});
$location.path('/foo/id3/eId'); $location.path('/foo/id3/eId');
$rootScope.$digest(); $rootScope.$digest();
expect($location.path()).toEqual('/bar/id3'); expect($location.path()).toEqual('/bar/id3');
expect(replace).toBe(true); expect($browserUrl.mostRecentCall.args)
.toEqual(['http://server/#/bar/id3?extra=eId', true]);
}); });
}); });
}); });