Skip to content

Commit b18d807

Browse files
committed
fix($state): allows superseding transitions from callbacks
Closes angular-ui#516
1 parent 62257a2 commit b18d807

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/state.js

+3
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
374374
}
375375
}
376376

377+
// Run it again, to catch any transitions in callbacks
378+
if ($state.transition !== transition) return TransitionSuperseded;
379+
377380
// Update globals in $state
378381
$state.$current = to;
379382
$state.current = to.self;

test/stateSpec.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ describe('state', function () {
5959
return "/templates/" + params.item + ".html";
6060
}
6161
})
62+
.state('home.redirect', {
63+
url: "redir",
64+
onEnter: function($state) {
65+
$state.transitionTo("about");
66+
}
67+
})
6268

6369
.state('first', { url: '^/first/subpath' })
6470
.state('second', { url: '^/second' });
@@ -306,8 +312,13 @@ describe('state', function () {
306312
$q.flush();
307313
expect($state.current).toBe(A);
308314
expect(resolvedError(superseded)).toBeTruthy();
309-
expect(log).toBe(
310-
'$stateChangeStart(B,A);');
315+
expect(log).toBe('$stateChangeStart(B,A);');
316+
}));
317+
318+
it('aborts pending transitions when aborted from callbacks', inject(function ($state, $q) {
319+
var superseded = $state.transitionTo('home.redirect');
320+
$q.flush();
321+
expect($state.current.name).toBe('about');
311322
}));
312323

313324
it('triggers onEnter and onExit callbacks', inject(function ($state, $q) {
@@ -557,6 +568,7 @@ describe('state', function () {
557568
'first',
558569
'home',
559570
'home.item',
571+
'home.redirect',
560572
'second'
561573
];
562574
expect(list.map(function(state) { return state.name; })).toEqual(names);

0 commit comments

Comments
 (0)