Skip to content

Commit

Permalink
feat($state): update transition redirect syntax
Browse files Browse the repository at this point in the history
Redirecting from a transition hook now looks like:

```
return $state.target(‘new.state’, { … });
```
  • Loading branch information
nateabele committed Jan 1, 2016
1 parent 2eb3769 commit 2ff3c8b
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/ng1/stateEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ import {Transition} from "../transition/transition";
$urlRouter.update();

function redirectFn():TargetState {
return $state.targetState(redirect.to, redirect.toParams, redirect.options);
return $state.target(redirect.to, redirect.toParams, redirect.options);
}

if (e.defaultPrevented) {
Expand Down
7 changes: 3 additions & 4 deletions src/state/hooks/transitionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Transition} from "../../transition/transition";
import {TransitionRejection, RejectType} from "../../transition/rejectFactory";

import {StateService, StateDeclaration} from "../interface";
import {TargetState} from "../targetState";
import {ViewHooks} from "./viewHooks";
import {EnterExitHooks} from "./enterExitHooks";
import {ResolveHooks} from "./resolveHooks";
Expand Down Expand Up @@ -94,10 +95,8 @@ export class TransitionManager {
return $state.current;
}

if (error.type === RejectType.SUPERSEDED) {
if (error.redirected && error.detail instanceof Transition) {
return this._redirectMgr(error.detail).runTransition();
}
if (error.type === RejectType.SUPERSEDED && error.redirected && error.detail instanceof TargetState) {
return this._redirectMgr(transition.redirect(error.detail)).runTransition();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/state/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ export interface StateService {
$current: State;
transition: Transition;
reload (stateOrName: StateOrName): IPromise<State>;
targetState (identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions): TargetState;
target (identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions): TargetState;
go (to: StateOrName, params: RawParams, options: TransitionOptions): IPromise<State>;
transitionTo (to: StateOrName, toParams: ParamsOrArray, options: TransitionOptions): IPromise<State>;
is (stateOrName: StateOrName, params?: RawParams, options?: TransitionOptions): boolean;
Expand Down
7 changes: 4 additions & 3 deletions src/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export function $StateProvider($urlRouterProvider, $urlMatcherFactoryProvider: U
}
let target = <TargetState> result;
// Recreate the TargetState, in case the state is now defined.
target = $state.targetState(target.identifier(), target.params(), target.options());
target = $state.target(target.identifier(), target.params(), target.options());

if (!target.valid()) return rejectFactory.invalid(target.error());
if (latestThing() !== latest) return rejectFactory.superseded();
Expand Down Expand Up @@ -542,7 +542,7 @@ export function $StateProvider($urlRouterProvider, $urlMatcherFactoryProvider: U
};

/** Factory method for creating a TargetState */
$state.targetState = function targetState(identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions = {}): TargetState {
$state.target = function target(identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions = {}): TargetState {
let stateDefinition = matcher.find(identifier, options.relative);
return new TargetState(identifier, stateDefinition, params, options);
};
Expand Down Expand Up @@ -593,10 +593,11 @@ export function $StateProvider($urlRouterProvider, $urlMatcherFactoryProvider: U
if (isObject(options.reload) && !(<any>options.reload).name)
throw new Error('Invalid reload state object');
options.reloadState = options.reload === true ? $state.$current.path[0] : matcher.find(<any> options.reload, options.relative);

if (options.reload && !options.reloadState)
throw new Error(`No such reload state '${(isString(options.reload) ? options.reload : (<any>options.reload).name)}'`);

let ref: TargetState = $state.targetState(to, toParams, options);
let ref: TargetState = $state.target(to, toParams, options);
let latestTreeChanges: TreeChanges = treeChangesQueue.peekTail();
let currentPath: Node[] = latestTreeChanges ? latestTreeChanges.to : rootPath();

Expand Down
4 changes: 1 addition & 3 deletions src/transition/rejectFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export class TransitionRejection {
}

toString() {
function detailString(d) {
return d && d.toString !== Object.prototype.toString ? d.toString() : JSON.stringify(d);
}
const detailString = d => d && d.toString !== Object.prototype.toString ? d.toString() : JSON.stringify(d);
let type = this.type, message = this.message, detail = detailString(this.detail);
return `TransitionRejection(type: ${type}, message: ${message}, detail: ${detail})`;
}
Expand Down
5 changes: 2 additions & 3 deletions src/transition/transitionHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import {IInjectable, defaults, extend, noop, filter, not, isFunction, isDefined,
import {trace} from "../common/trace";
import {services} from "../common/coreservices";

import {Transition} from "./transition";
import {TransitionRejection, RejectFactory} from "./rejectFactory";
import {State} from "../state/module";
import {State, TargetState} from "../state/module";
import {Resolvable, ResolveContext} from "../resolve/module";

let REJECT = new RejectFactory();
Expand Down Expand Up @@ -42,7 +41,7 @@ export class TransitionHook {
// If the hook returns false, abort the current Transition
[eq(false), val(REJECT.aborted("Hook aborted transition"))],
// If the hook returns a Transition, halt the current Transition and redirect to that Transition.
[is(Transition), (transition) => REJECT.redirected(transition)],
[is(TargetState), (target) => REJECT.redirected(target)],
// A promise was returned, wait for the promise and then chain another hookHandler
[isPromise, (promise) => promise.then(this.handleHookResult.bind(this))]
]);
Expand Down
2 changes: 1 addition & 1 deletion test/resolveSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ describe("Integration: Resolvables system", () => {
it("should not re-resolve data, when redirecting to a child", () => {
$transitions.onStart({to: "J"}, ($transition$, _J) => {
expect(counts._J).toEqualData(1);
return $transition$.redirect($state.targetState("K"));
return $state.target("K");
});
$state.go("J");
$rootScope.$digest();
Expand Down
2 changes: 1 addition & 1 deletion test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1885,7 +1885,7 @@ describe('.onInvalid()', function() {

it('should allow redirection if an ITargetState is returned', inject(function($state, $transitions, $q) {
$stateProvider.onInvalid(function($to$) {
return $state.targetState("second", $to$.params(), $to$.options());
return $state.target("second", $to$.params(), $to$.options());
});

$state.go("invalid");
Expand Down
5 changes: 2 additions & 3 deletions test/transitionSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,15 @@ describe('transition', function () {
var states = [], rejection, transition = makeTransition("A", "D");
transitionProvider.onEnter({ from: "*", to: "*" }, function($state$) { states.push($state$); });
transitionProvider.onEnter({ from: "*", to: "C" }, function($state, $transition$) {
return $transition$.redirect(targetState("B"));
return targetState("B");
});
transition.promise.catch(function(err) { rejection = err; });

transition.run(); $q.flush();

expect(pluck(states, 'name')).toEqual([ 'B', 'C' ]);
expect(rejection.type).toEqual(RejectType.SUPERSEDED);
expect(rejection.detail.to().name).toEqual("B");
expect(rejection.detail.from().name).toEqual("A");
expect(rejection.detail.name()).toEqual("B");
expect(rejection.redirected).toEqual(true);
}));

Expand Down

0 comments on commit 2ff3c8b

Please sign in to comment.