Skip to content

Commit 8a23707

Browse files
committed
docs(router): improve documentation for router bindings
1 parent c01f327 commit 8a23707

File tree

1 file changed

+100
-10
lines changed

1 file changed

+100
-10
lines changed

modules/angular2/router.ts

+100-10
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,101 @@ import {Location} from './src/router/location';
3131
import {bind, OpaqueToken, Binding} from './core';
3232
import {CONST_EXPR, Type} from './src/core/facade/lang';
3333

34+
/**
35+
* Token used to bind the component with the top-level {@link RouteConfig}s for the
36+
* application.
37+
*
38+
* You can use the {@link routerBindings} function in your {@link bootstrap} bindings to
39+
* simplify setting up these bindings.
40+
*
41+
* ## Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
42+
*
43+
* ```
44+
* import {Component, View} from 'angular2/angular2';
45+
* import {
46+
* ROUTER_DIRECTIVES,
47+
* ROUTER_BINDINGS,
48+
* ROUTER_PRIMARY_COMPONENT,
49+
* RouteConfig
50+
* } from 'angular2/router';
51+
*
52+
* @Component({...})
53+
* @View({directives: [ROUTER_DIRECTIVES]})
54+
* @RouteConfig([
55+
* {...},
56+
* ])
57+
* class AppCmp {
58+
* // ...
59+
* }
60+
*
61+
* bootstrap(AppCmp, [
62+
* ROUTER_BINDINGS,
63+
* bind(ROUTER_PRIMARY_COMPONENT).toValue(AppCmp)
64+
* ]);
65+
* ```
66+
*/
3467
export const ROUTER_PRIMARY_COMPONENT: OpaqueToken =
3568
CONST_EXPR(new OpaqueToken('RouterPrimaryComponent'));
3669

37-
export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);
38-
3970
/**
40-
* A list of {@link Binding}. To use the router, you must add this to your application.
71+
* A list of directives. To use the router directives like {@link RouterOutlet} and
72+
* {@link RouterLink}, add this to your `directives` array in the {@link View} decorator of your
73+
* component.
4174
*
42-
* ## Example
75+
* ## Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
76+
*
77+
* ```
78+
* import {Component, View} from 'angular2/angular2';
79+
* import {ROUTER_DIRECTIVES, routerBindings, RouteConfig} from 'angular2/router';
4380
*
44-
* ```typescript
4581
* @Component({...})
4682
* @View({directives: [ROUTER_DIRECTIVES]})
4783
* @RouteConfig([
48-
* new Route(...),
84+
* {...},
4985
* ])
5086
* class AppCmp {
51-
* constructor(router: Router, location: Location) {
5287
* // ...
53-
* }
54-
*
5588
* }
5689
*
57-
*
5890
* bootstrap(AppCmp, [routerBindings(AppCmp)]);
5991
* ```
6092
*/
93+
export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);
94+
95+
/**
96+
* A list of {@link Binding}s. To use the router, you must add this to your application.
97+
*
98+
* Note that you also need to bind to {@link ROUTER_PRIMARY_COMPONENT}.
99+
*
100+
* You can use the {@link routerBindings} function in your {@link bootstrap} bindings to
101+
* simplify setting up these bindings.
102+
*
103+
* ## Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
104+
*
105+
* ```
106+
* import {Component, View} from 'angular2/angular2';
107+
* import {
108+
* ROUTER_DIRECTIVES,
109+
* ROUTER_BINDINGS,
110+
* ROUTER_PRIMARY_COMPONENT,
111+
* RouteConfig
112+
* } from 'angular2/router';
113+
*
114+
* @Component({...})
115+
* @View({directives: [ROUTER_DIRECTIVES]})
116+
* @RouteConfig([
117+
* {...},
118+
* ])
119+
* class AppCmp {
120+
* // ...
121+
* }
122+
*
123+
* bootstrap(AppCmp, [
124+
* ROUTER_BINDINGS,
125+
* bind(ROUTER_PRIMARY_COMPONENT).toValue(AppCmp)
126+
* ]);
127+
* ```
128+
*/
61129
export const ROUTER_BINDINGS: any[] = CONST_EXPR([
62130
RouteRegistry,
63131
CONST_EXPR(new Binding(LocationStrategy, {toClass: PathLocationStrategy})),
@@ -74,6 +142,28 @@ function routerFactory(registry, location, primaryComponent) {
74142
return new RootRouter(registry, location, primaryComponent);
75143
}
76144

145+
/**
146+
* A list of {@link Binding}s. To use the router, you must add these bindings to
147+
* your application.
148+
*
149+
* ## Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
150+
*
151+
* ```
152+
* import {Component, View} from 'angular2/angular2';
153+
* import {ROUTER_DIRECTIVES, routerBindings, RouteConfig} from 'angular2/router';
154+
*
155+
* @Component({...})
156+
* @View({directives: [ROUTER_DIRECTIVES]})
157+
* @RouteConfig([
158+
* {...},
159+
* ])
160+
* class AppCmp {
161+
* // ...
162+
* }
163+
*
164+
* bootstrap(AppCmp, [routerBindings(AppCmp)]);
165+
* ```
166+
*/
77167
export function routerBindings(primaryComponent: Type): Array<any> {
78168
return [ROUTER_BINDINGS, bind(ROUTER_PRIMARY_COMPONENT).toValue(primaryComponent)];
79169
}

0 commit comments

Comments
 (0)