Skip to content

Commit

Permalink
feat(Transition): Add Transition.originalTransition() to return the i…
Browse files Browse the repository at this point in the history
…nitial transition in a chain of redirects
  • Loading branch information
christopherthielen committed Jan 5, 2017
1 parent e659227 commit 4fe39e3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/hooks/lazyLoad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ import { StateRule } from "../url/interface";
* See [[StateDeclaration.lazyLoad]]
*/
const lazyLoadHook: TransitionHookFn = (transition: Transition) => {
const transitionSource = (trans: Transition) =>
trans.redirectedFrom() ? transitionSource(trans.redirectedFrom()) : trans.options().source;
let router = transition.router;

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

return services.$q.all(promises).then(retryOriginalTransition);
return services.$q.all(promises).then(retryTransition);
};

export const registerLazyLoadHook = (transitionService: TransitionService) =>
Expand Down
36 changes: 33 additions & 3 deletions src/transition/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,13 @@ export class Transition implements IHookRegistry {
}

/**
* If the current transition is a redirect, returns the transition that was redirected.
*
* Gets the transition from which this transition was redirected.
*
* If the current transition is a redirect, this method returns the transition that was redirected.
*
* #### Example:
* ```js
* let transitionA = $state.go('A').transitionA
* let transitionA = $state.go('A').transition
* transitionA.onStart({}, () => $state.target('B'));
* $transitions.onSuccess({ to: 'B' }, (trans) => {
* trans.to().name === 'B'; // true
Expand All @@ -389,6 +388,37 @@ export class Transition implements IHookRegistry {
return this._options.redirectedFrom || null;
}

/**
* Gets the original transition in a redirect chain
*
* A transition might belong to a long chain of multiple redirects.
* This method walks the [[redirectedFrom]] chain back to the original (first) transition in the chain.
*
* #### Example:
* ```js
* // states
* registry.register({ name: 'A', redirectTo: 'B' });
* registry.register({ name: 'B', redirectTo: 'C' });
* registry.register({ name: 'C', redirectTo: 'D' });
* registry.register({ name: 'D' });
*
* let transitionA = $state.go('A').transition
*
* $transitions.onSuccess({ to: 'D' }, (trans) => {
* trans.to().name === 'D'; // true
* trans.redirectedFrom().to().name === 'C'; // true
* trans.originalTransition() === transitionA; // true
* trans.originalTransition().to().name === 'A'; // true
* });
* ```
*
* @returns The original Transition that started a redirect chain
*/
originalTransition(): Transition {
let rf = this.redirectedFrom();
return (rf && rf.originalTransition()) || this;
}

/**
* Get the transition options
*
Expand Down

0 comments on commit 4fe39e3

Please sign in to comment.