Skip to content

Commit

Permalink
feat($state): broadcast $stateChangeCancel event when event.preventDe…
Browse files Browse the repository at this point in the history
…fault() is called in $stateChangeStart handler
  • Loading branch information
rpocklin committed Mar 28, 2015
1 parent 3e06565 commit ecefb75
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
* </pre>
*/
if ($rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams).defaultPrevented) {
$rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams);
$urlRouter.update();
return TransitionPrevented;
}
Expand Down
20 changes: 20 additions & 0 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ describe('state', function () {
log = '';
logEvents = logEnterExit = false;
$rootScope.$on('$stateChangeStart', eventLogger);
$rootScope.$on('$stateChangeCancel', eventLogger);
$rootScope.$on('$stateChangeSuccess', eventLogger);
$rootScope.$on('$stateChangeError', eventLogger);
$rootScope.$on('$stateNotFound', eventLogger);
Expand Down Expand Up @@ -215,6 +216,25 @@ describe('state', function () {
expect(resolvedError(promise)).toBeTruthy();
}));

it('can be cancelled by preventDefault() in $stateChangeStart and broadcasts $stateChangeCancel', inject(function ($state, $q, $rootScope) {
initStateTo(A);
var called, cancelEventCalled;
$rootScope.$on('$stateChangeStart', function (ev) {
ev.preventDefault();
called = true;
});
$rootScope.$on('$stateChangeCancel', function (ev) {
ev.preventDefault();
cancelEventCalled = true;
});
var promise = $state.transitionTo(B, {});
$q.flush();
expect(called).toBeTruthy();
expect(cancelEventCalled).toBeTruthy();
expect($state.current).toBe(A);
expect(resolvedError(promise)).toBeTruthy();
}));

it('triggers $stateNotFound', inject(function ($state, $q, $rootScope) {
initStateTo(E, { i: 'iii' });
var called;
Expand Down

0 comments on commit ecefb75

Please sign in to comment.