Skip to content

Commit

Permalink
feat($state): allow prevent syncUrl on failure
Browse files Browse the repository at this point in the history
Check defaultPrevented before syncUrl on failure.
  • Loading branch information
iamdey committed Mar 19, 2014
1 parent 5ee8d1e commit 753060b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -946,8 +946,11 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
* @param {Object} fromParams The params supplied to the `fromState`.
* @param {Error} error The resolve error object.
*/
$rootScope.$broadcast('$stateChangeError', to.self, toParams, from.self, fromParams, error);
syncUrl();
evt = $rootScope.$broadcast('$stateChangeError', to.self, toParams, from.self, fromParams, error);

if (!evt.defaultPrevented) {
syncUrl();
}

return $q.reject(error);
});
Expand Down
15 changes: 15 additions & 0 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,21 @@ describe('state', function () {
expect($state.current.name).toBe("about");
}));

it('should not revert to last known working url on state change failure', inject(function ($state, $rootScope, $location, $q) {
$state.transitionTo("about");
$q.flush();

$rootScope.$on("$stateChangeError", function(event){
event.defaultPrevented = true;
});

$location.path("/resolve-fail");
$rootScope.$broadcast("$locationChangeSuccess");
$rootScope.$apply();

expect($location.path()).toBe("/resolve-fail");
}));

it('should replace browser history when "replace" enabled', inject(function ($state, $rootScope, $location, $q) {
var originalReplaceFn = $location.replace, replaceWasCalled = false;

Expand Down

0 comments on commit 753060b

Please sign in to comment.