Skip to content

Commit

Permalink
fix($state): statechangeCancel: don't clobber url if a new transition…
Browse files Browse the repository at this point in the history
… has started

Closes #2238
Closes #2229
Closes #2185
Closes #2236
Closes #2098
Closes #600
  • Loading branch information
christopherthielen committed Jan 23, 2016
1 parent 350d3e8 commit e00aa69
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
*/
if ($rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams, options).defaultPrevented) {
$rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams);
$urlRouter.update();
//Don't update and resync url if there's been a new transition started. see issue #2238, #600
if ($state.transition == null) $urlRouter.update();
return TransitionPrevented;
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1648,3 +1648,32 @@ describe('$stateParams', function () {
expect($stateParams.foo).toBeUndefined();
}));
});

// Test for #600, #2238, #2229
describe('otherwise and state redirects', function() {
beforeEach(module(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('home', { url: '/home', template: 'home' })
.state('loginPage', { url: '/login', templateUrl: 'login.html' });
}));

beforeEach(inject(function ($rootScope, $state) {

$rootScope.$on('$stateChangeStart', function (event, toState) {
if (toState.name !== "loginPage") {
event.preventDefault();
$state.go('loginPage', { redirectUrl: toState.name });
}
});
}));

iit("should not go into an infinite loop", inject(function($location, $rootScope, $state, $urlRouter, $httpBackend) {
$httpBackend.expectGET("login.html").respond("login page");
$location.url("notmatched");
$urlRouter.update(true);
expect(function() { $httpBackend.flush(); }).not.toThrow();
expect(function() { $rootScope.$digest(); }).not.toThrow();
expect($state.current.name).toBe("loginPage")
}));
});

0 comments on commit e00aa69

Please sign in to comment.