Skip to content

Commit 998c7c2

Browse files
committed
doc: add some API doc
Closes angular#4060
1 parent 5e013c4 commit 998c7c2

File tree

9 files changed

+164
-33
lines changed

9 files changed

+164
-33
lines changed

modules/angular2/router.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ import {CONST_EXPR} from './src/core/facade/lang';
3838

3939
export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);
4040

41+
/**
42+
* A list of {@link Binding}. To use the router, you must add this to your application.
43+
*
44+
* ## Example
45+
*
46+
* ```typescript
47+
* @Component({...})
48+
* @View({directives: [ROUTER_DIRECTIVES]})
49+
* @RouteConfig([
50+
* new Route(...),
51+
* ])
52+
* class AppCmp {
53+
* constructor(router: Router, location: Location) {
54+
* // ...
55+
* }
56+
*
57+
* }
58+
*
59+
*
60+
* bootstrap(AppCmp, [ROUTER_BINDINGS]);
61+
* ```
62+
*/
4163
export const ROUTER_BINDINGS: any[] = CONST_EXPR([
4264
RouteRegistry,
4365
Pipeline,

modules/angular2/src/core/application_tokens.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {CONST_EXPR} from 'angular2/src/core/facade/lang';
77
export const APP_COMPONENT_REF_PROMISE = CONST_EXPR(new OpaqueToken('Promise<ComponentRef>'));
88

99
/**
10-
* An opaque token representing the application root type in the {@link Injector}.
10+
* An {@link angular2/di/OpaqueToken} representing the application root type in the {@link
11+
* Injector}.
1112
*
1213
* ```
1314
* @Component(...)

modules/angular2/src/core/change_detection/pipe_transform.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
import {ABSTRACT, BaseException, CONST, Type} from 'angular2/src/core/facade/lang';
22

33
/**
4-
* An interface which all pipes must implement.
4+
* To create a Pipe, you must implement this interface.
55
*
6-
* #Example
6+
* Angular invokes the `transform` method with the subject as the `value` argument and any
7+
* parameters in the `args` Array.
8+
*
9+
* ## Syntax
10+
*
11+
* `subject | pipeName[:arg0[:arg1...]]`
12+
*
13+
* ## Example
14+
*
15+
* The `RepeatPipe` below repeats the subject as many times as indicated by the first argument:
716
*
817
* ```
9-
* class DoublePipe implements PipeTransform {
10-
* transform(value, args = []) {
11-
* return `${value}${value}`;
18+
* @Pipe({name: 'repeat'})
19+
* class RepeatPipe implements PipeTransform {
20+
*
21+
* transform(value: any, args: any[] = []) {
22+
* if (isBlank(args) || args.length == 0) {
23+
* throw new BaseException('limitTo pipe requires one argument');
24+
* }
25+
*
26+
* let times: number = args[0];
27+
*
28+
* return value.repeat(times);
1229
* }
1330
* }
1431
* ```
32+
*
33+
* Invoking `{{ 'ok' | repeat:3 }}` in a template produces `okokok`.
34+
*
35+
* ```
1536
*/
1637
export interface PipeTransform { transform(value: any, args: any[]): any; }
1738

modules/angular2/src/core/compiler/element_ref.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import {ViewRef} from './view_ref';
33
import {RenderViewRef, RenderElementRef, Renderer} from 'angular2/src/core/render/api';
44

55
/**
6-
* Reference to the element.
6+
* An opaque reference to the underlying element.
77
*
8-
* Represents an opaque reference to the underlying element. The element is a DOM ELement in
9-
* a Browser, but may represent other types on other rendering platforms. In the browser the
10-
* `ElementRef` can be sent to the web-worker. Web Workers can not have references to the
11-
* DOM Elements.
8+
* The underlying native element is a DOM Element in a browser context, but may represent other
9+
* types on other rendering platforms. In the browser the `ElementRef` can be sent to the Web
10+
* Worker. Web Workers can not have references to the DOM Elements.
1211
*/
1312
export class ElementRef implements RenderElementRef {
1413
/**
@@ -51,11 +50,11 @@ export class ElementRef implements RenderElementRef {
5150
/**
5251
* Returns the native Element implementation.
5352
*
54-
* In the browser this represents the DOM Element.
53+
* In the browser this represents the DOM element.
5554
*
5655
* The `nativeElement` can be used as an escape hatch when direct DOM manipulation is needed. Use
57-
* this with caution, as it creates tight coupling between your application and the Browser, which
58-
* will not work in WebWorkers.
56+
* this with caution, as it creates tight coupling between your application and the browser, which
57+
* will not work in Web Workers.
5958
*
6059
* NOTE: This method will return null in the webworker scenario!
6160
*/

modules/angular2/src/core/di/binding.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,38 @@ export class ResolvedFactory {
266266
/**
267267
* Provides an API for imperatively constructing {@link Binding}s.
268268
*
269-
* This is only relevant for JavaScript. See {@link BindingBuilder}.
269+
* To construct a {@link Binding}, bind a `token` to either a class, a value or a factory function.
270+
* See {@link BindingBuilder} for more details.
271+
*
272+
* The `token` is most commonly an {@link angular2/di/OpaqueToken} or a class.
273+
*
274+
* `bind` is only relevant for JavaScript. For Dart use the {@link Binding} constructor.
270275
*
271276
* ## Example
272277
*
273-
* ```javascript
274-
* bind(MyInterface).toClass(MyClass)
278+
* ```typescript
279+
* // inj.get(MyClass) would instantiate MyClass
280+
* bind(MyClass).toClass(MyClass);
281+
*
282+
* // inj.get(MyClass) === 'my class'
283+
* bind(MyClass).toValue('my class');
275284
*
285+
* // inj.get(MyClass) would instantiate the depenency and call the factory function with the
286+
* // instance
287+
* bind(MyClass).toFactory(dep => new MyClass(dep), [DepClass]);
288+
*
289+
* // inj.get(MyOtherClass) === inj.get(MyClass)
290+
* bind(MyOtherClass).toAlias(MyClass);
291+
* ```
292+
*
293+
* ```dart
294+
* var binding = new Binding(MyClass, toClass: MyClass);
295+
* var binding = new Binding(MyClass, toValue: 'my class');
296+
* var binding = new Binding(MyClass, toFactory: (dep) => new MyClass(dep),
297+
* dependencies: [DepClass]);
298+
* var binding = new Binding(MyOtherClass, toAlias: MyClass);
276299
* ```
300+
*
277301
*/
278302
export function bind(token): BindingBuilder {
279303
return new BindingBuilder(token);

modules/angular2/src/core/di/injector.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,21 @@ const _MAX_CONSTRUCTION_COUNTER = 10;
2727

2828
export const UNDEFINED: Object = CONST_EXPR(new Object());
2929

30+
/**
31+
* Visibility of a {@link Binding}.
32+
*/
3033
export enum Visibility {
34+
/**
35+
* A `Public` {@link Binding} is only visible to regular (as opposed to host) child injectors.
36+
*/
3137
Public,
38+
/**
39+
* A `Private` {@link Binding} is only visible to host (as opposed to regular) child injectors.
40+
*/
3241
Private,
42+
/**
43+
* A `PublicAndPrivate` {@link Binding} is visible to both host and regular child injectors.
44+
*/
3345
PublicAndPrivate
3446
}
3547

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
import {CONST} from 'angular2/src/core/facade/lang';
22

3+
/**
4+
* By binding to an `OpaqueToken` you can enable an application to return more meaningful error
5+
* messages.
6+
*
7+
* ## Example
8+
*
9+
* ```
10+
* // While the following would work, see below for the preferred way
11+
* var binding = bind('value0').toValue(0);
12+
* ...
13+
* var value = injector.get('value0');
14+
*
15+
* // An OpaqueToken is the preferred way and lead to more helpful error messages
16+
* export value0Token = new OpaqueToken('value0');
17+
* var binding = bind(value0Token).toValue(0);
18+
* ...
19+
* var value = injector.get(value0Token);
20+
* ```
21+
*/
322
@CONST()
423
export class OpaqueToken {
5-
_desc: string;
24+
constructor(private _desc: string) {}
625

7-
constructor(desc: string) { this._desc = 'Token(' + desc + ')'; }
8-
9-
toString(): string { return this._desc; }
26+
toString(): string { return `Token ${this._desc}`; }
1027
}

modules/angular2/src/core/facade/async.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,46 @@ export class Observable {
9090
}
9191

9292
/**
93+
* Use by directives and components to emit custom {@link Event}s.
94+
*
95+
* ## Examples
96+
*
97+
* In the following example, `Zippy` alternatively emits `open` and `close` events when its
98+
* title gets clicked:
99+
*
100+
* ```
101+
* @Component({selector: 'zippy'})
102+
* @View({template: `
103+
* <div class="zippy">
104+
* <div (click)="toggle()">Toggle</div>
105+
* <div [hidden]="!visible">
106+
* <ng-content></ng-content>
107+
* </div>
108+
* </div>`})
109+
* export class Zippy {
110+
* visible: boolean = true;
111+
* @Event() open: EventEmitter = new EventEmitter();
112+
* @Event() close: EventEmitter = new EventEmitter();
113+
*
114+
* toggle() {
115+
* this.visible = !this.visible;
116+
* if (this.visible) {
117+
* this.open.next(null);
118+
* } else {
119+
* this.close.next(null);
120+
* }
121+
* }
122+
* }
123+
* ```
124+
*
93125
* Use Rx.Observable but provides an adapter to make it work as specified here:
94126
* https://github.com/jhusain/observable-spec
95127
*
96128
* Once a reference implementation of the spec is available, switch to it.
97129
*/
98130
export class EventEmitter extends Observable {
99-
_subject: Rx.Subject<any>;
100-
_immediateScheduler;
101-
102-
constructor() {
103-
super();
104-
this._subject = new Rx.Subject<any>();
105-
this._immediateScheduler = (<any>Rx.Scheduler).immediate;
106-
}
131+
_subject: Rx.Subject<any> = new Rx.Subject<any>();
132+
_immediateScheduler = (<any>Rx.Scheduler).immediate;
107133

108134
observer(generator: any): Rx.IDisposable {
109135
return this._subject.observeOn(this._immediateScheduler)
@@ -114,9 +140,18 @@ export class EventEmitter extends Observable {
114140

115141
toRx(): Rx.Observable<any> { return this._subject; }
116142

143+
/**
144+
* Emits a `value`.
145+
*/
117146
next(value: any) { this._subject.onNext(value); }
118147

148+
/**
149+
* Emits an `error`.
150+
*/
119151
throw(error: any) { this._subject.onError(error); }
120152

153+
/**
154+
* Closes the stream.
155+
*/
121156
return (value?: any) { this._subject.onCompleted(); }
122157
}

modules/angular2/src/router/router.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ let _resolveToTrue = PromiseWrapper.resolve(true);
2727
let _resolveToFalse = PromiseWrapper.resolve(false);
2828

2929
/**
30-
* # Router
31-
* The router is responsible for mapping URLs to components.
30+
* The `Router` is responsible for mapping URLs to components.
3231
*
3332
* You can see the state of the router by inspecting the read-only field `router.navigating`.
3433
* This may be useful for showing a spinner, for instance.
3534
*
3635
* ## Concepts
36+
*
3737
* Routers and component instances have a 1:1 correspondence.
3838
*
39-
* The router holds reference to a number of "outlets." An outlet is a placeholder that the
40-
* router dynamically fills in depending on the current URL.
39+
* The router holds reference to a number of {@link RouterOutlet}.
40+
* An outlet is a placeholder that the router dynamically fills in depending on the current URL.
4141
*
4242
* When the router navigates from a URL, it must first recognizes it and serialize it into an
4343
* `Instruction`.

0 commit comments

Comments
 (0)