Skip to content

Commit 7ee295a

Browse files
committed
docs(compiler): compiler related api docs
1 parent 0319417 commit 7ee295a

File tree

7 files changed

+177
-75
lines changed

7 files changed

+177
-75
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ export class CompilerCache {
7070
}
7171
}
7272

73-
/**
74-
*
73+
/*
7574
* ## URL Resolution
7675
*
7776
* ```
@@ -88,9 +87,15 @@ export class CompilerCache {
8887
* var url = viewAnnotation.templateUrl;
8988
* var componentUrl = componentUrlMapper.getUrl(componentType);
9089
* var componentResolvedUrl = urlResolver.resolve(appRootUrl.value, componentUrl);
91-
* var templateResolvedUrl = urlResolver.resolve(componetResolvedUrl, url);
90+
* var templateResolvedUrl = urlResolver.resolve(componentResolvedUrl, url);
9291
* ```
9392
*/
93+
/**
94+
* Service for compiling {@link Component}s so that they can be later instantiated and their
95+
* {@link View}s can be rendered.
96+
*
97+
* <!-- TODO: check Component and View links, they should likely go to glossary instead -->
98+
*/
9499
@Injectable()
95100
export class Compiler {
96101
private _compiling: Map<Type, Promise<AppProtoView>> = new Map();
@@ -127,8 +132,10 @@ export class Compiler {
127132
return PipeBinding.createFromType(typeOrBinding, meta);
128133
}
129134

130-
// Create a hostView as if the compiler encountered <hostcmp></hostcmp>.
131-
// Used for bootstrapping.
135+
/**
136+
* Compiles an {@link EntryPointComponent entry-point Component} and returns a promise for a
137+
* {@link ProtoViewRef} that can be passed into {@link AppViewManager
138+
*/
132139
compileInHost(componentType: Type): Promise<ProtoViewRef> {
133140
var r = wtfStartTimeRange('Compiler#compile()', stringify(componentType));
134141

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import {ElementRef} from './element_ref';
77
import {ViewRef, HostViewRef} from './view_ref';
88

99
/**
10-
* Angular's reference to a component instance.
11-
*
12-
* `ComponentRef` represents a component instance lifecycle and meta information.
10+
* Represents a component instance as a node in application's component tree and provides access to
11+
* other objects related to this component instance.
1312
*/
1413
export class ComponentRef {
1514
/**
@@ -24,7 +23,6 @@ export class ComponentRef {
2423

2524
componentType: Type;
2625

27-
2826
injector: Injector;
2927

3028
/**
@@ -54,11 +52,15 @@ export class ComponentRef {
5452
}
5553

5654
/**
57-
* Service for dynamically loading a Component into an arbitrary position in the internal Angular
58-
* application tree.
55+
* Service for creating an instance of a component and attaching it to the component tree of an
56+
* application at a specified location.
5957
*/
6058
@Injectable()
6159
export class DynamicComponentLoader {
60+
61+
/**
62+
* @private
63+
*/
6264
constructor(private _compiler: Compiler, private _viewManager: AppViewManager) {}
6365

6466
/**

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

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

55
/**
6-
* An opaque reference to the underlying element.
6+
* Represents a location in a View that has an injection, change-detection and render contexts
7+
* associated with it.
78
*
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.
9+
* An `ElementRef` is created for each element in the Template that contains a Directive, Component
10+
* or data-binding.
11+
*
12+
* An `ElementRef` is backed by a render-specific element. In the browser context, this is usually a
13+
* DOM element.
1114
*/
1215
export class ElementRef implements RenderElementRef {
16+
1317
/**
14-
* Reference to the {@link ViewRef} where the `ElementRef` is inside of.
18+
* @private
19+
*
20+
* Reference to the {@link ViewRef} that this `ElementRef` is part of.
1521
*/
1622
parentView: ViewRef;
1723

1824

1925
/**
26+
* @private
27+
*
2028
* Index of the element inside the {@link ViewRef}.
2129
*
2230
* This is used internally by the Angular framework to locate elements.
2331
*/
2432
boundElementIndex: number;
2533

2634
/**
35+
* @private
36+
*
2737
* Index of the element inside the `RenderViewRef`.
2838
*
2939
* This is used internally by the Angular framework to locate elements.
3040
*/
3141
renderBoundElementIndex: number;
3242

43+
/**
44+
* @private
45+
*/
3346
constructor(parentView: ViewRef, boundElementIndex: number, renderBoundElementIndex: number,
3447
private _renderer: Renderer) {
3548
this.parentView = parentView;
@@ -38,7 +51,7 @@ export class ElementRef implements RenderElementRef {
3851
}
3952

4053
/**
41-
*
54+
* @private
4255
*/
4356
get renderView(): RenderViewRef { return this.parentView.render; }
4457

@@ -48,15 +61,22 @@ export class ElementRef implements RenderElementRef {
4861
set renderView(viewRef: RenderViewRef) { throw new BaseException('Abstract setter'); }
4962

5063
/**
51-
* Returns the native Element implementation.
52-
*
53-
* In the browser this represents the DOM element.
54-
*
55-
* The `nativeElement` can be used as an escape hatch when direct DOM manipulation is needed. Use
56-
* this with caution, as it creates tight coupling between your application and the browser, which
57-
* will not work in Web Workers.
64+
* The underlying native element or `null` if direct access to native elements is not supported
65+
* (e.g. when the application runs in a web worker).
5866
*
59-
* NOTE: This method will return null in the webworker scenario!
67+
* <div class="callout is-critical">
68+
* <header>Use with caution</header>
69+
* <p>
70+
* Use this api as the last resort when direct access to DOM is needed. Use templating and
71+
* data-binding provided by Angular instead.
72+
* </p>
73+
* <p>
74+
* Relying on direct DOM access creates tight coupling between your application and rendering
75+
* layers which will make it impossible to separate the two and deploy your application in into a
76+
* web worker.
77+
* </p>
78+
* <!-- TODO: add info about custom renderers that should be used instead -->
79+
* </div>
6080
*/
6181
get nativeElement(): any { return this._renderer.getNativeElementSync(this); }
6282
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@ import {ElementRef} from './element_ref';
33
import * as viewModule from './view';
44

55
/**
6-
* Reference to a template within a component.
6+
* Represents an Embedded Template that can be used for creating Embedded Views.
77
*
8-
* Represents an opaque reference to the underlying template that can
9-
* be instantiated using the {@link ViewContainerRef}.
8+
* Use {@link ViewContainerRef#createEmbeddedView} method to instantiate an Embedded View and attach
9+
* it to a View Container.
10+
*
11+
* <!-- TODO: how to get hold of it? -->
1012
*/
1113
export class TemplateRef {
14+
1215
/**
13-
* The location of the template
16+
* The location in the View where the Embedded View logically belong to.
17+
*
18+
* This `ElementRef` provides the data-binding and injection context for Embedded Views created
19+
* from this `TemplateRef`.
1420
*/
1521
elementRef: ElementRef;
1622

23+
/**
24+
* @private
25+
*/
1726
constructor(elementRef: ElementRef) { this.elementRef = elementRef; }
1827

1928
private _getProtoView(): viewModule.AppProtoView {
@@ -23,10 +32,14 @@ export class TemplateRef {
2332
.nestedProtoView;
2433
}
2534

35+
/**
36+
* Reference to the ProtoView created by compiling the original Embedded Template, from which the
37+
* Embedded View is instatiated.
38+
*/
2639
get protoViewRef(): ProtoViewRef { return this._getProtoView().ref; }
2740

2841
/**
29-
* Whether this template has a local variable with the given name
42+
* Returns true if the Template declares a Local Variable with the given name.
3043
*/
3144
hasLocal(name: string): boolean { return this._getProtoView().variableBindings.has(name); }
3245
}

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

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,52 @@ import {TemplateRef} from './template_ref';
1010
import {ViewRef, HostViewRef, ProtoViewRef, internalView} from './view_ref';
1111

1212
/**
13-
* A location where {@link ViewRef}s can be attached.
13+
* Represents a container where one or more Views can be attached.
1414
*
15-
* A `ViewContainerRef` represents a location in a {@link ViewRef} where other child
16-
* {@link ViewRef}s can be inserted. Adding and removing views is the only way of structurally
17-
* changing the rendered DOM of the application.
15+
* The container can contain two kinds of Views. Host Views, created by instantiating a
16+
* {@link Component} via {@link #createHostView}s, and Embedded Views, created by instantiating an
17+
* {@link TemplateRef Embedded Template} via {@link #createEmbeddedView}).
18+
*
19+
* The location of the View Container within the containing View is specified by the Anchor
20+
* `element`. Each View Container can have only one Anchor Element and each Anchor Element can only
21+
* have a single View Container.
22+
*
23+
* Root elements of Views attached to this container become siblings of the Anchor Element in
24+
* the Rendered View.
25+
*
26+
* To access a ViewContainerRef of an Element, you can either place a {@link Directive} injected
27+
* with `ViewContainerRef` on the Element, or you obtain it via
28+
* {@link ViewManager#getViewContainer}.
29+
*
30+
* <!-- TODO: Is ViewContainerRef#element a public api? Should it be renamed? to anchor or anchorElementRef? -->
31+
* <!-- TODO: rename all atIndex params to just index or get(index) to get(atIndex) -->
1832
*/
1933
export class ViewContainerRef {
34+
2035
/**
2136
* @private
2237
*/
23-
constructor(public viewManager: avmModule.AppViewManager, public element: ElementRef) {}
38+
constructor(
39+
/**
40+
* @private
41+
*/
42+
public viewManager: avmModule.AppViewManager,
43+
44+
/**
45+
* Anchor element that specifies the location of this container in the containing View.
46+
*/
47+
public element: ElementRef
48+
) {
49+
50+
}
2451

2552
private _getViews(): Array<viewModule.AppView> {
2653
var vc = internalView(this.element.parentView).viewContainers[this.element.boundElementIndex];
2754
return isPresent(vc) ? vc.views : [];
2855
}
2956

3057
/**
31-
* Remove all {@link ViewRef}s at current location.
58+
* Destroys all Views in the container.
3259
*/
3360
clear(): void {
3461
for (var i = this.length - 1; i >= 0; i--) {
@@ -37,28 +64,22 @@ export class ViewContainerRef {
3764
}
3865

3966
/**
40-
* Return a {@link ViewRef} at specific index.
67+
* Returns the {@link ViewRef} for the View located in this container at the specified index.
4168
*/
4269
get(index: number): ViewRef { return this._getViews()[index].ref; }
4370

4471
/**
45-
* Returns number of {@link ViewRef}s currently attached at this location.
72+
* Returns the number of Views currently attached to this container.
4673
*/
4774
get length(): number { return this._getViews().length; }
4875

4976
/**
50-
* Create and insert a {@link ViewRef} into the view-container.
77+
* Instantiates an Embedded View based on the {@link TemplateRef `templateRef`} and inserts it
78+
* into this container at index specified via `atIndex`.
5179
*
52-
* - `protoViewRef` (optional) {@link ProtoViewRef} - The `ProtoView` to use for creating
53-
* `View` to be inserted at this location. If `ViewContainer` is created at a location
54-
* of inline template, then `protoViewRef` is the `ProtoView` of the template.
55-
* - `atIndex` (optional) `number` - location of insertion point. (Or at the end if unspecified.)
56-
* - `context` (optional) {@link ElementRef} - Context (for expression evaluation) from the
57-
* {@link ElementRef} location. (Or current context if unspecified.)
58-
* - `bindings` (optional) Array of {@link ResolvedBinding} - Used for configuring
59-
* `ElementInjector`.
80+
* If `atIndex` is not specified, the new View will be inserted as the last View in the container.
6081
*
61-
* Returns newly created {@link ViewRef}.
82+
* Returns the {@link ViewRef} for the newly created View.
6283
*/
6384
// TODO(rado): profile and decide whether bounds checks should be added
6485
// to the methods below.
@@ -67,6 +88,20 @@ export class ViewContainerRef {
6788
return this.viewManager.createEmbeddedViewInContainer(this.element, atIndex, templateRef);
6889
}
6990

91+
/**
92+
* Instantiates a single {@link Component} and inserts it into this container at index specified
93+
* via `atIndex`.
94+
*
95+
* The component is instantiated using its {@link ProtoViewRef `protoViewRef`} which can be
96+
* obtained via {@link Compiler#compileInHost}.
97+
*
98+
* If `atIndex` is not specified, the new View will be inserted as the last View in the container.
99+
*
100+
* You can optionally specify `dynamicallyCreatedBindings`, which configure the {@link Injector}
101+
* that will be created for the new Host View. <!-- TODO: what is host view? it's never defined!!! -->
102+
*
103+
* Returns the {@link ViewRef} for the newly created View.
104+
*/
70105
createHostView(protoViewRef: ProtoViewRef = null, atIndex: number = -1,
71106
dynamicallyCreatedBindings: ResolvedBinding[] = null): HostViewRef {
72107
if (atIndex == -1) atIndex = this.length;
@@ -75,29 +110,32 @@ export class ViewContainerRef {
75110
}
76111

77112
/**
78-
* Insert a {@link ViewRef} at specefic index.
113+
* <!-- TODO: refactor into move and remove -->
114+
* Inserts a View identified by a {@link ViewRef} into the container at index specified via
115+
* `atIndex`.
79116
*
80-
* The index is location at which the {@link ViewRef} should be attached. If omitted it is
81-
* inserted at the end.
117+
* If `atIndex` is not specified, the new View will be inserted as the last View in the container.
82118
*
83119
* Returns the inserted {@link ViewRef}.
120+
* <!-- TODO: why does it return ViewRef? looks useless -->
84121
*/
85122
insert(viewRef: ViewRef, atIndex: number = -1): ViewRef {
86123
if (atIndex == -1) atIndex = this.length;
87124
return this.viewManager.attachViewInContainer(this.element, atIndex, viewRef);
88125
}
89126

90127
/**
91-
* Return the index of already inserted {@link ViewRef}.
128+
* Returns the index of the View, specified via {@link ViewRef}, within the current container.
92129
*/
93130
indexOf(viewRef: ViewRef): number {
94131
return ListWrapper.indexOf(this._getViews(), internalView(viewRef));
95132
}
96133

97134
/**
98-
* Remove a {@link ViewRef} at specific index.
135+
* <!-- TODO: rename to destroy -->
136+
* Destroys a View attached to this container at index specified via `atIndex`.
99137
*
100-
* If the index is omitted last {@link ViewRef} is removed.
138+
* If `atIndex` is not specified, the last View in the container will be removed.
101139
*/
102140
remove(atIndex: number = -1): void {
103141
if (atIndex == -1) atIndex = this.length - 1;
@@ -106,8 +144,11 @@ export class ViewContainerRef {
106144
}
107145

108146
/**
109-
* The method can be used together with insert to implement a view move, i.e.
110-
* moving the DOM nodes while the directives in the view stay intact.
147+
* <!-- TODO: refactor into move and remove -->
148+
* Use along with {@link #insert} to move a View within the current container.
149+
*
150+
* If the `atIndex` param is omitted, the last {@link ViewRef} is detached.
151+
* <!-- TODO: why does it return ViewRef? looks useless -->
111152
*/
112153
detach(atIndex: number = -1): ViewRef {
113154
if (atIndex == -1) atIndex = this.length - 1;

0 commit comments

Comments
 (0)