-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(redirectTo): Process
redirectTo
property of a state as a redir…
- Loading branch information
1 parent
771c4ab
commit 6becb12
Showing
4 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {isString, isFunction} from "../common/predicates" | ||
import {UIRInjector} from "../common/interface"; | ||
import {Transition} from "../transition/transition"; | ||
import {UiRouter} from "../router"; | ||
import {services} from "../common/coreservices"; | ||
import {TargetState} from "../state/targetState"; | ||
|
||
/** | ||
* A hook that redirects to a different state or params | ||
* | ||
* See [[StateDeclaration.redirectTo]] | ||
*/ | ||
export const redirectToHook = (transition: Transition, $injector: UIRInjector) => { | ||
let redirect = transition.to().redirectTo; | ||
if (!redirect) return; | ||
|
||
let router: UiRouter = $injector.get(UiRouter); | ||
let $state = router.stateService; | ||
|
||
if (isFunction(redirect)) | ||
return services.$q.when(redirect(transition, $injector)).then(handleResult); | ||
|
||
return handleResult(redirect); | ||
|
||
function handleResult(result) { | ||
if (result instanceof TargetState) return result; | ||
if (isString(result)) return $state.target(<any> result, transition.params(), transition.options()); | ||
if (result['state'] || result['params']) | ||
return $state.target(result['state'] || transition.to(), result['params'] || transition.params(), transition.options()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
|
||
import {UiRouter} from "../../src/router"; | ||
import {tree2Array} from "../stateHelper.ts"; | ||
import {find} from "../../src/common/common"; | ||
|
||
|
||
let statetree = { | ||
A: { | ||
AA: { | ||
AAA: { | ||
url: "/:fooId", params: { fooId: "" } | ||
} | ||
} | ||
} | ||
}; | ||
|
||
describe("hooks", () => { | ||
let router, $state, states, init; | ||
beforeEach(() => { | ||
router = new UiRouter(); | ||
$state = router.stateService; | ||
states = tree2Array(statetree, false); | ||
init = () => { | ||
states.forEach(state => router.stateRegistry.register(state)); | ||
router.stateRegistry.stateQueue.autoFlush($state); | ||
} | ||
}) | ||
|
||
describe('redirectTo:', () => { | ||
it("should redirect to a state by name from the redirectTo: string", (done) => { | ||
find(states, s => s.name === 'A').redirectTo = "AAA"; | ||
init(); | ||
|
||
$state.go('A').then(() => { | ||
expect(router.globals.current.name).toBe('AAA') | ||
done() | ||
}) | ||
}) | ||
|
||
it("should redirect to a state by name from the redirectTo: object", (done) => { | ||
find(states, s => s.name === 'A').redirectTo = { state: "AAA" } | ||
init(); | ||
|
||
$state.go('A').then(() => { | ||
expect(router.globals.current.name).toBe('AAA') | ||
done() | ||
}) | ||
}) | ||
|
||
it("should redirect to a state and params by name from the redirectTo: object", (done) => { | ||
find(states, s => s.name === 'A').redirectTo = { state: "AAA", params: { fooId: 'abc'} }; | ||
init(); | ||
|
||
$state.go('A').then(() => { | ||
expect(router.globals.current.name).toBe('AAA') | ||
expect(router.globals.params.fooId).toBe('abc') | ||
done() | ||
}) | ||
}) | ||
|
||
it("should redirect to a TargetState returned from the redirectTo: function", (done) => { | ||
find(states, s => s.name === 'A').redirectTo = | ||
() => $state.target("AAA"); | ||
init(); | ||
|
||
$state.go('A').then(() => { | ||
expect(router.globals.current.name).toBe('AAA') | ||
done() | ||
}) | ||
}) | ||
|
||
it("should redirect after waiting for a promise for a state name returned from the redirectTo: function", (done) => { | ||
find(states, s => s.name === 'A').redirectTo = () => new Promise((resolve) => { | ||
setTimeout(() => resolve("AAA"), 50) | ||
}); | ||
init(); | ||
|
||
$state.go('A').then(() => { | ||
expect(router.globals.current.name).toBe('AAA'); | ||
done() | ||
}) | ||
}) | ||
|
||
it("should redirect after waiting for a promise for a {state, params} returned from the redirectTo: function", (done) => { | ||
find(states, s => s.name === 'A').redirectTo = () => new Promise((resolve) => { | ||
setTimeout(() => resolve({ state: "AAA", params: { fooId: "FOO" } }), 50) | ||
}); | ||
init(); | ||
|
||
$state.go('A').then(() => { | ||
expect(router.globals.current.name).toBe('AAA'); | ||
expect(router.globals.params.fooId).toBe('FOO'); | ||
done() | ||
}) | ||
}) | ||
|
||
it("should redirect after waiting for a promise for a TargetState returned from the redirectTo: function", (done) => { | ||
find(states, s => s.name === 'A').redirectTo = () => new Promise((resolve) => { | ||
setTimeout(() => resolve($state.target("AAA")), 50) | ||
}); | ||
init(); | ||
|
||
$state.go('A').then(() => { | ||
expect(router.globals.current.name).toBe('AAA'); | ||
done() | ||
}) | ||
}) | ||
|
||
it("should not redirect if the redirectTo: function returns something other than a string, { state, params}, TargetState (or promise for)", (done) => { | ||
find(states, s => s.name === 'A').redirectTo = () => new Promise((resolve) => { | ||
setTimeout(() => resolve(12345), 50) | ||
}); | ||
init(); | ||
|
||
$state.go('A').then(() => { | ||
expect(router.globals.current.name).toBe('A'); | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
}); |