Skip to content

Commit 4fe39e3

Browse files
feat(Transition): Add Transition.originalTransition() to return the initial transition in a chain of redirects
1 parent e659227 commit 4fe39e3

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/hooks/lazyLoad.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ import { StateRule } from "../url/interface";
3232
* See [[StateDeclaration.lazyLoad]]
3333
*/
3434
const lazyLoadHook: TransitionHookFn = (transition: Transition) => {
35-
const transitionSource = (trans: Transition) =>
36-
trans.redirectedFrom() ? transitionSource(trans.redirectedFrom()) : trans.options().source;
3735
let router = transition.router;
3836

39-
function retryOriginalTransition() {
40-
if (transitionSource(transition) !== 'url') {
37+
function retryTransition() {
38+
if (transition.originalTransition().options().source !== 'url') {
4139
// The original transition was not triggered via url sync
4240
// The lazy state should be loaded now, so re-try the original transition
4341
let orig = transition.targetState();
@@ -66,7 +64,7 @@ const lazyLoadHook: TransitionHookFn = (transition: Transition) => {
6664
.filter(state => !!state.lazyLoad)
6765
.map(state => lazyLoadState(transition, state));
6866

69-
return services.$q.all(promises).then(retryOriginalTransition);
67+
return services.$q.all(promises).then(retryTransition);
7068
};
7169

7270
export const registerLazyLoadHook = (transitionService: TransitionService) =>

src/transition/transition.ts

+33-3
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,13 @@ export class Transition implements IHookRegistry {
368368
}
369369

370370
/**
371-
* If the current transition is a redirect, returns the transition that was redirected.
372-
*
373371
* Gets the transition from which this transition was redirected.
374372
*
373+
* If the current transition is a redirect, this method returns the transition that was redirected.
375374
*
376375
* #### Example:
377376
* ```js
378-
* let transitionA = $state.go('A').transitionA
377+
* let transitionA = $state.go('A').transition
379378
* transitionA.onStart({}, () => $state.target('B'));
380379
* $transitions.onSuccess({ to: 'B' }, (trans) => {
381380
* trans.to().name === 'B'; // true
@@ -389,6 +388,37 @@ export class Transition implements IHookRegistry {
389388
return this._options.redirectedFrom || null;
390389
}
391390

391+
/**
392+
* Gets the original transition in a redirect chain
393+
*
394+
* A transition might belong to a long chain of multiple redirects.
395+
* This method walks the [[redirectedFrom]] chain back to the original (first) transition in the chain.
396+
*
397+
* #### Example:
398+
* ```js
399+
* // states
400+
* registry.register({ name: 'A', redirectTo: 'B' });
401+
* registry.register({ name: 'B', redirectTo: 'C' });
402+
* registry.register({ name: 'C', redirectTo: 'D' });
403+
* registry.register({ name: 'D' });
404+
*
405+
* let transitionA = $state.go('A').transition
406+
*
407+
* $transitions.onSuccess({ to: 'D' }, (trans) => {
408+
* trans.to().name === 'D'; // true
409+
* trans.redirectedFrom().to().name === 'C'; // true
410+
* trans.originalTransition() === transitionA; // true
411+
* trans.originalTransition().to().name === 'A'; // true
412+
* });
413+
* ```
414+
*
415+
* @returns The original Transition that started a redirect chain
416+
*/
417+
originalTransition(): Transition {
418+
let rf = this.redirectedFrom();
419+
return (rf && rf.originalTransition()) || this;
420+
}
421+
392422
/**
393423
* Get the transition options
394424
*

0 commit comments

Comments
 (0)