Skip to content

Commit 2ff3c8b

Browse files
committed
feat($state): update transition redirect syntax
Redirecting from a transition hook now looks like: ``` return $state.target(‘new.state’, { … }); ```
1 parent 2eb3769 commit 2ff3c8b

File tree

9 files changed

+16
-20
lines changed

9 files changed

+16
-20
lines changed

src/ng1/stateEvents.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ import {Transition} from "../transition/transition";
166166
$urlRouter.update();
167167

168168
function redirectFn():TargetState {
169-
return $state.targetState(redirect.to, redirect.toParams, redirect.options);
169+
return $state.target(redirect.to, redirect.toParams, redirect.options);
170170
}
171171

172172
if (e.defaultPrevented) {

src/state/hooks/transitionManager.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {Transition} from "../../transition/transition";
99
import {TransitionRejection, RejectType} from "../../transition/rejectFactory";
1010

1111
import {StateService, StateDeclaration} from "../interface";
12+
import {TargetState} from "../targetState";
1213
import {ViewHooks} from "./viewHooks";
1314
import {EnterExitHooks} from "./enterExitHooks";
1415
import {ResolveHooks} from "./resolveHooks";
@@ -94,10 +95,8 @@ export class TransitionManager {
9495
return $state.current;
9596
}
9697

97-
if (error.type === RejectType.SUPERSEDED) {
98-
if (error.redirected && error.detail instanceof Transition) {
99-
return this._redirectMgr(error.detail).runTransition();
100-
}
98+
if (error.type === RejectType.SUPERSEDED && error.redirected && error.detail instanceof TargetState) {
99+
return this._redirectMgr(transition.redirect(error.detail)).runTransition();
101100
}
102101
}
103102

src/state/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export interface StateService {
420420
$current: State;
421421
transition: Transition;
422422
reload (stateOrName: StateOrName): IPromise<State>;
423-
targetState (identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions): TargetState;
423+
target (identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions): TargetState;
424424
go (to: StateOrName, params: RawParams, options: TransitionOptions): IPromise<State>;
425425
transitionTo (to: StateOrName, toParams: ParamsOrArray, options: TransitionOptions): IPromise<State>;
426426
is (stateOrName: StateOrName, params?: RawParams, options?: TransitionOptions): boolean;

src/state/state.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export function $StateProvider($urlRouterProvider, $urlMatcherFactoryProvider: U
371371
}
372372
let target = <TargetState> result;
373373
// Recreate the TargetState, in case the state is now defined.
374-
target = $state.targetState(target.identifier(), target.params(), target.options());
374+
target = $state.target(target.identifier(), target.params(), target.options());
375375

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

544544
/** Factory method for creating a TargetState */
545-
$state.targetState = function targetState(identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions = {}): TargetState {
545+
$state.target = function target(identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions = {}): TargetState {
546546
let stateDefinition = matcher.find(identifier, options.relative);
547547
return new TargetState(identifier, stateDefinition, params, options);
548548
};
@@ -593,10 +593,11 @@ export function $StateProvider($urlRouterProvider, $urlMatcherFactoryProvider: U
593593
if (isObject(options.reload) && !(<any>options.reload).name)
594594
throw new Error('Invalid reload state object');
595595
options.reloadState = options.reload === true ? $state.$current.path[0] : matcher.find(<any> options.reload, options.relative);
596+
596597
if (options.reload && !options.reloadState)
597598
throw new Error(`No such reload state '${(isString(options.reload) ? options.reload : (<any>options.reload).name)}'`);
598599

599-
let ref: TargetState = $state.targetState(to, toParams, options);
600+
let ref: TargetState = $state.target(to, toParams, options);
600601
let latestTreeChanges: TreeChanges = treeChangesQueue.peekTail();
601602
let currentPath: Node[] = latestTreeChanges ? latestTreeChanges.to : rootPath();
602603

src/transition/rejectFactory.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ export class TransitionRejection {
2222
}
2323

2424
toString() {
25-
function detailString(d) {
26-
return d && d.toString !== Object.prototype.toString ? d.toString() : JSON.stringify(d);
27-
}
25+
const detailString = d => d && d.toString !== Object.prototype.toString ? d.toString() : JSON.stringify(d);
2826
let type = this.type, message = this.message, detail = detailString(this.detail);
2927
return `TransitionRejection(type: ${type}, message: ${message}, detail: ${detail})`;
3028
}

src/transition/transitionHook.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import {IInjectable, defaults, extend, noop, filter, not, isFunction, isDefined,
66
import {trace} from "../common/trace";
77
import {services} from "../common/coreservices";
88

9-
import {Transition} from "./transition";
109
import {TransitionRejection, RejectFactory} from "./rejectFactory";
11-
import {State} from "../state/module";
10+
import {State, TargetState} from "../state/module";
1211
import {Resolvable, ResolveContext} from "../resolve/module";
1312

1413
let REJECT = new RejectFactory();
@@ -42,7 +41,7 @@ export class TransitionHook {
4241
// If the hook returns false, abort the current Transition
4342
[eq(false), val(REJECT.aborted("Hook aborted transition"))],
4443
// If the hook returns a Transition, halt the current Transition and redirect to that Transition.
45-
[is(Transition), (transition) => REJECT.redirected(transition)],
44+
[is(TargetState), (target) => REJECT.redirected(target)],
4645
// A promise was returned, wait for the promise and then chain another hookHandler
4746
[isPromise, (promise) => promise.then(this.handleHookResult.bind(this))]
4847
]);

test/resolveSpec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ describe("Integration: Resolvables system", () => {
635635
it("should not re-resolve data, when redirecting to a child", () => {
636636
$transitions.onStart({to: "J"}, ($transition$, _J) => {
637637
expect(counts._J).toEqualData(1);
638-
return $transition$.redirect($state.targetState("K"));
638+
return $state.target("K");
639639
});
640640
$state.go("J");
641641
$rootScope.$digest();

test/stateSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1885,7 +1885,7 @@ describe('.onInvalid()', function() {
18851885

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

18911891
$state.go("invalid");

test/transitionSpec.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,15 @@ describe('transition', function () {
309309
var states = [], rejection, transition = makeTransition("A", "D");
310310
transitionProvider.onEnter({ from: "*", to: "*" }, function($state$) { states.push($state$); });
311311
transitionProvider.onEnter({ from: "*", to: "C" }, function($state, $transition$) {
312-
return $transition$.redirect(targetState("B"));
312+
return targetState("B");
313313
});
314314
transition.promise.catch(function(err) { rejection = err; });
315315

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

318318
expect(pluck(states, 'name')).toEqual([ 'B', 'C' ]);
319319
expect(rejection.type).toEqual(RejectType.SUPERSEDED);
320-
expect(rejection.detail.to().name).toEqual("B");
321-
expect(rejection.detail.from().name).toEqual("A");
320+
expect(rejection.detail.name()).toEqual("B");
322321
expect(rejection.redirected).toEqual(true);
323322
}));
324323

0 commit comments

Comments
 (0)