From 753060b910d5d2da600a6fa0757976e401c33172 Mon Sep 17 00:00:00 2001 From: David Epely Date: Tue, 18 Mar 2014 10:52:45 +0100 Subject: [PATCH] feat($state): allow prevent syncUrl on failure Check defaultPrevented before syncUrl on failure. --- src/state.js | 7 +++++-- test/stateSpec.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/state.js b/src/state.js index 5857deaa4..326e00a7e 100644 --- a/src/state.js +++ b/src/state.js @@ -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); }); diff --git a/test/stateSpec.js b/test/stateSpec.js index d91c49d53..08d0f8601 100644 --- a/test/stateSpec.js +++ b/test/stateSpec.js @@ -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;