Skip to content

Commit ec68074

Browse files
committed
docs(router): improve docs for Instruction and related classes
Closes angular#4303
1 parent 3d6e3c2 commit ec68074

1 file changed

Lines changed: 80 additions & 4 deletions

File tree

modules/angular2/src/router/instruction.ts

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,79 @@ import {Promise} from 'angular2/src/core/facade/async';
1111
import {PathRecognizer} from './path_recognizer';
1212
import {Url} from './url_parser';
1313

14+
/**
15+
* `RouteParams` is an immutable map of parameters for the given route
16+
* based on the url matcher and optional parameters for that route.
17+
*
18+
* You can inject `RouteParams` into the constructor of a component to use it.
19+
*
20+
* ## Example
21+
*
22+
* ```
23+
* import {bootstrap, Component, View} from 'angular2/angular2';
24+
* import {Router, ROUTER_DIRECTIVES, routerBindings, RouteConfig} from 'angular2/router';
25+
*
26+
* @Component({...})
27+
* @View({directives: [ROUTER_DIRECTIVES]})
28+
* @RouteConfig([
29+
* {path: '/user/:id', component: UserCmp, as: 'UserCmp'},
30+
* ])
31+
* class AppCmp {}
32+
*
33+
* @Component({...})
34+
* @View({ template: 'user: {{id}}' })
35+
* class UserCmp {
36+
* string: id;
37+
* constructor(params: RouteParams) {
38+
* this.id = params.get('id');
39+
* }
40+
* }
41+
*
42+
* bootstrap(AppCmp, routerBindings(AppCmp));
43+
* ```
44+
*/
1445
export class RouteParams {
1546
constructor(public params: StringMap<string, string>) {}
1647

1748
get(param: string): string { return normalizeBlank(StringMapWrapper.get(this.params, param)); }
1849
}
1950

2051
/**
21-
* `Instruction` is a tree of `ComponentInstructions`, with all the information needed
52+
* `Instruction` is a tree of {@link ComponentInstruction}s with all the information needed
2253
* to transition each component in the app to a given route, including all auxiliary routes.
2354
*
24-
* This is a public API.
55+
* `Instruction`s can be created using {@link Router#generate}, and can be used to
56+
* perform route changes with {@link Router#navigateByInstruction}.
57+
*
58+
* ## Example
59+
*
60+
* ```
61+
* import {bootstrap, Component, View} from 'angular2/angular2';
62+
* import {Router, ROUTER_DIRECTIVES, routerBindings, RouteConfig} from 'angular2/router';
63+
*
64+
* @Component({...})
65+
* @View({directives: [ROUTER_DIRECTIVES]})
66+
* @RouteConfig([
67+
* {...},
68+
* ])
69+
* class AppCmp {
70+
* constructor(router: Router) {
71+
* var instruction = router.generate(['/MyRoute']);
72+
* router.navigateByInstruction(instruction);
73+
* }
74+
* }
75+
*
76+
* bootstrap(AppCmp, routerBindings(AppCmp));
77+
* ```
2578
*/
2679
export class Instruction {
2780
constructor(public component: ComponentInstruction, public child: Instruction,
2881
public auxInstruction: StringMap<string, Instruction>) {}
2982

83+
/**
84+
* Returns a new instruction that shares the state of the existing instruction, but with
85+
* the given child {@link Instruction} replacing the existing child.
86+
*/
3087
replaceChild(child: Instruction): Instruction {
3188
return new Instruction(this.component, child, this.auxInstruction);
3289
}
@@ -85,8 +142,7 @@ function stringifyAux(instruction: Instruction): string {
85142
*
86143
* `ComponentInstruction`s are [https://en.wikipedia.org/wiki/Hash_consing](hash consed). You should
87144
* never construct one yourself with "new." Instead, rely on {@link Router/PathRecognizer} to
88-
* construct
89-
* `ComponentInstruction`s.
145+
* construct `ComponentInstruction`s.
90146
*
91147
* You should not modify this object. It should be treated as immutable.
92148
*/
@@ -99,13 +155,33 @@ export class ComponentInstruction {
99155
constructor(public urlPath: string, public urlParams: string[],
100156
private _recognizer: PathRecognizer, public params: StringMap<string, any> = null) {}
101157

158+
/**
159+
* Returns the component type of the represented route, or `null` if this instruction
160+
* hasn't been resolved.
161+
*/
102162
get componentType() { return this._recognizer.handler.componentType; }
103163

164+
/**
165+
* Returns a promise that will resolve to component type of the represented route.
166+
* If this instruction references an {@link AsyncRoute}, the `loader` function of that route
167+
* will run.
168+
*/
104169
resolveComponentType(): Promise<Type> { return this._recognizer.handler.resolveComponentType(); }
105170

171+
/**
172+
* Returns the specificity of the route associated with this `Instruction`.
173+
*/
106174
get specificity() { return this._recognizer.specificity; }
107175

176+
/**
177+
* Returns `true` if the component type of this instruction has no child {@link RouteConfig},
178+
* or `false` if it does.
179+
*/
108180
get terminal() { return this._recognizer.terminal; }
109181

182+
/**
183+
* Returns the route data of the given route that was specified in the {@link RouteDefinition},
184+
* or `null` if no route data was specified.
185+
*/
110186
routeData(): Object { return this._recognizer.handler.data; }
111187
}

0 commit comments

Comments
 (0)