Skip to content

Commit c16b9e6

Browse files
feat(UIRouterConfig): Define UIRouterConfig class for router bootstrap
1 parent ff54d61 commit c16b9e6

File tree

4 files changed

+103
-9
lines changed

4 files changed

+103
-9
lines changed

src/ng2.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ import "./justjs";
99

1010
export * from "./ng2/providers";
1111
export * from "./ng2/directives";
12+
export * from "./ng2/uiRouterConfig";
1213

src/ng2/providers.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,29 @@ import {ViewService} from "../view/view";
1010
import {UiView} from "./uiView";
1111
import {ng2ViewsBuilder, Ng2ViewConfig} from "./viewsBuilder";
1212
import {Ng2ViewDeclaration} from "./interface";
13+
import {UIRouterConfig} from "./uiRouterConfig";
1314

14-
export const UIROUTER_PROVIDERS: Provider[] = [
15+
let uiRouterFactory = (routerConfig: UIRouterConfig) => {
16+
let router = new UIRouter();
17+
18+
router.viewService.viewConfigFactory("ng2", (node: Node, config: Ng2ViewDeclaration) => new Ng2ViewConfig(node, config));
19+
router.stateRegistry.decorator('views', ng2ViewsBuilder);
20+
21+
router.stateRegistry.stateQueue.autoFlush(router.stateService);
1522

16-
provide(UIRouter, { useFactory: () => {
17-
let router = new UIRouter();
23+
routerConfig.configure(router);
1824

19-
router.viewService.viewConfigFactory("ng2", (node: Node, config: Ng2ViewDeclaration) => new Ng2ViewConfig(node, config));
20-
router.stateRegistry.decorator('views', ng2ViewsBuilder);
21-
router.stateRegistry.stateQueue.autoFlush(router.stateService);
25+
if (!router.urlRouterProvider.interceptDeferred) {
26+
router.urlRouter.listen();
27+
router.urlRouter.sync();
28+
}
29+
30+
return router;
31+
};
32+
33+
export const UIROUTER_PROVIDERS: Provider[] = [
2234

23-
return router;
24-
} }),
35+
provide(UIRouter, { useFactory: uiRouterFactory, deps: [UIRouterConfig] }),
2536

2637
provide(StateService, { useFactory: (r: UIRouter) => { return r.stateService; }, deps: [UIRouter]}),
2738

src/ng2/uiRouterConfig.ts

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {UIRouter} from "../router";
2+
/**
3+
* Provides states configuration to UI-Router during application bootstrap.
4+
*
5+
* An instance of this class should be `provide()`d to the application `bootstrap()`.
6+
*
7+
* @example
8+
* ```js
9+
* import {UIROUTER_PROVIDERS, UiView} from "ui-router-ng2";
10+
* import {MyConfig} from "./app/myConfig";
11+
*
12+
* bootstrap(UiView, [
13+
* ...UIROUTER_PROVIDERS,
14+
* provide(UIRouterConfig, { useClass: MyConfig }
15+
* ]);
16+
* ```
17+
*
18+
* The application's initial states should be registered with the [[UIRouter.stateRegistry]].
19+
* Any global configuration (transition hooks, parameter types, etc) should be done here.
20+
*
21+
* @example
22+
* ```js
23+
*
24+
* // myconfig.ts
25+
* import {STATES} from "./states";
26+
* import {registerAuthHook} from "./hooks";
27+
* import {registerSlugType} from "./paramtypes";
28+
*
29+
* export class MyConfig {
30+
* configure(uiRouter: UIRouter) {
31+
* STATES.forEach(state => uiRouter.stateRegistry.register(state));
32+
* registerAuthHook(uiRouter.transitionService);
33+
* registerSlugType(uiRouter.urlMatcherFactory);
34+
* }
35+
* }
36+
*
37+
* // states.ts
38+
* import {FooComponent} from "./foo.component";
39+
* import {BarComponent} from "./bar.component";
40+
* import BAZ_MODULE_STATES from "./baz/states";
41+
*
42+
* export let STATES = [
43+
* { name: 'foo', url: '/url', component: FooComponent},
44+
* { name: 'bar', url: '/bar', component: BarComponent}
45+
* ].concat(BAZ_MODULE_STATES);
46+
*
47+
* // hooks.ts
48+
* export function registerAuthHook(transitionService: TransitionService) {
49+
* let requireAuthentication = ($state, AuthService) {
50+
* if (!AuthService.isAuthenticated()) {
51+
* return $state.target('login');
52+
* }
53+
* }
54+
* transitionService.onBefore({ to: (state) => state.requiresAuth }, requireAuthentication);
55+
* }
56+
*
57+
*
58+
* // paramtypes.ts
59+
* export function registerSlugType(urlMatcherFactory: UrlMatcherFactory) {
60+
* let builtInStringType = urlMatcherFactory.type('string');
61+
* let slugType = Object.assign({}, builtInStringType, { encode: (str) => str, decode: (str) => str });
62+
* urlMatcherFactory.type('slug', slugType);
63+
* }
64+
* ```
65+
*
66+
*/
67+
export class UIRouterConfig {
68+
/**
69+
* Configures UI-Router before bootstrap
70+
*
71+
* An app should perform UI-Router configuration here, such as registering the initial set of states,
72+
* parameter types, defining global hooks, etc.
73+
*
74+
* @param uiRouter the uiRouter instance being configured
75+
*/
76+
public configure(uiRouter: UIRouter) {
77+
78+
}
79+
}

src/url/urlRouter.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,12 @@ function update(rules: Function[], otherwiseFn: Function, evt?: any) {
7676
* in your module config.
7777
*/
7878
export class UrlRouterProvider {
79+
/** @hidden */
7980
rules = [];
81+
/** @hidden */
8082
otherwiseFn: Function = null;
81-
private interceptDeferred = false;
83+
/** @hidden */
84+
interceptDeferred = false;
8285

8386
constructor(private $urlMatcherFactory: UrlMatcherFactory, private $stateParams: StateParams) {
8487

0 commit comments

Comments
 (0)