Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($location): reset $location.$$replace with every watch call
Browse files Browse the repository at this point in the history
Closes #1111
  • Loading branch information
rkirov authored and IgorMinar committed Nov 26, 2012
1 parent cfe13b5 commit a32bc40
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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;
});
Expand Down
23 changes: 23 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));


Expand Down
13 changes: 4 additions & 9 deletions test/ng/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
});
});
Expand Down

0 comments on commit a32bc40

Please sign in to comment.