Skip to content

Commit 06f8330

Browse files
committed
fix(core): Document the new bootstrap APIs. Also rename rootBindings() to platformBindings() to be more clear about what it is.
Closes angular#4218
1 parent f490565 commit 06f8330

File tree

3 files changed

+141
-28
lines changed

3 files changed

+141
-28
lines changed

modules/angular2/src/core/application_common.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,38 @@ export function applicationDomBindings(): Array<Type | Binding | any[]> {
9292
];
9393
}
9494

95-
96-
9795
/**
98-
* Initialize the Angular context on the page.
96+
* Initialize the Angular 'platform' on the page.
97+
*
98+
* See {@link PlatformRef} for details on the Angular platform.
99+
*
100+
* # Without specified bindings
101+
*
102+
* If no bindings are specified, `platform`'s behavior depends on whether an existing
103+
* platform exists:
104+
*
105+
* If no platform exists, a new one will be created with the default {@link platformBindings}.
106+
*
107+
* If a platform already exists, it will be returned (regardless of what bindings it
108+
* was created with). This is a convenience feature, allowing for multiple applications
109+
* to be loaded into the same platform without awareness of each other.
110+
*
111+
* # With specified bindings
112+
*
113+
* It is also possible to specify bindings to be made in the new platform. These bindings
114+
* will be shared between all applications on the page. For example, an abstraction for
115+
* the browser cookie jar should be bound at the platform level, because there is only one
116+
* cookie jar regardless of how many applications on the age will be accessing it.
117+
*
118+
* If bindings are specified directly, `platform` will create the Angular platform with
119+
* them if a platform did not exist already. If it did exist, however, an error will be
120+
* thrown.
121+
*
122+
* # DOM Applications
99123
*
100-
* If no bindings are provided, calling {@link platform}() is idempotent,
101-
* and will use the default platform bindings (which can be obtained from
102-
* {@link ApplicationRef/rootBindings}).
124+
* This version of `platform` initializes Angular to run in the UI thread, with direct
125+
* DOM access. Web-worker applications should call `platform` from
126+
* `src/web_workers/worker/application_common` instead.
103127
*/
104128
export function platform(bindings?: Array<Type | Binding | any[]>): PlatformRef {
105129
return platformCommon(bindings, () => {

modules/angular2/src/core/application_ref.ts

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import {NgZone} from 'angular2/src/core/zone/ng_zone';
32
import {Type, isBlank, isPresent, assertionsEnabled} from 'angular2/src/core/facade/lang';
43
import {bind, Binding, Injector, OpaqueToken} from 'angular2/src/core/di';
@@ -47,9 +46,12 @@ import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mappe
4746

4847

4948
/**
50-
* Contains everything that is safe to share between applications.
49+
* Constructs the set of bindings meant for use at the platform level.
50+
*
51+
* These are bindings that should be singletons shared among all Angular applications
52+
* running on the page.
5153
*/
52-
export function rootBindings(): Array<Type | Binding | any[]> {
54+
export function platformBindings(): Array<Type | Binding | any[]> {
5355
return [bind(Reflector).toValue(reflector), TestabilityRegistry];
5456
}
5557

@@ -145,18 +147,19 @@ export function platformCommon(bindings?: Array<Type | Binding | any[]>, initial
145147
}
146148

147149
if (isBlank(bindings)) {
148-
bindings = rootBindings();
150+
bindings = platformBindings();
149151
}
150152
_platform = new PlatformRef(Injector.resolveAndCreate(bindings), () => { _platform = null; });
151153
return _platform;
152154
}
153155

154156
/**
155-
* Represent the Angular context on a page, and is a true singleton.
157+
* The Angular platform is the entry point for Angular on a web page. Each page
158+
* has exactly one platform, and services (such as reflection) which are common
159+
* to every Angular application running on the page are bound in its scope.
156160
*
157-
* The platform {@link Injector} injects dependencies which are also
158-
* truly singletons in the context of a page (such as the browser's
159-
* cookie jar).
161+
* A page's platform is initialized implicitly when {@link bootstrap}() is called, or
162+
* explicitly by calling {@link platform}().
160163
*/
161164
export class PlatformRef {
162165
/**
@@ -170,25 +173,55 @@ export class PlatformRef {
170173
constructor(private _injector: Injector, private _dispose: () => void) {}
171174

172175
/**
173-
* Get the platform {@link Injector}.
176+
* Retrieve the platform {@link Injector}, which is the parent injector for
177+
* every Angular application on the page and provides singleton bindings.
174178
*/
175179
get injector(): Injector { return this._injector; }
176180

177181
/**
178-
* Build a new Angular application with the given bindings. The `ApplicationRef`
179-
* returned can be used to bootstrap one or more root components within the
180-
* application.
182+
* Instantiate a new Angular application on the page.
183+
*
184+
* # What is an application?
185+
*
186+
* Each Angular application has its own zone, change detection, compiler,
187+
* renderer, and other framework components. An application hosts one or more
188+
* root components, which can be initialized via `ApplicationRef.bootstrap()`.
189+
*
190+
* # Application Bindings
191+
*
192+
* Angular applications require numerous bindings to be properly instantiated.
193+
* When using `application()` to create a new app on the page, these bindings
194+
* must be provided. Fortunately, there are helper functions to configure
195+
* typical bindings, as shown in the example below.
196+
*
197+
* # Example
198+
* ```
199+
* var myAppBindings = [MyAppService];
200+
*
201+
* platform()
202+
* .application([applicationCommonBindings(), applicationDomBindings(), myAppBindings])
203+
* .bootstrap(MyTopLevelComponent);
204+
* ```
205+
* # See Also
206+
*
207+
* See the {@link bootstrap} documentation for more details.
181208
*/
182209
application(bindings: Array<Type | Binding | any[]>): ApplicationRef {
183210
var app = this._initApp(createNgZone(), bindings);
184211
return app;
185212
}
186213

187214
/**
188-
* Build a new Angular application from asynchronously provided bindings.
215+
* Instantiate a new Angular application on the page, using bindings which
216+
* are only available asynchronously. One such use case is to initialize an
217+
* application running in a web worker.
189218
*
190-
* Runs the `AsyncLoader` callback in the application `Zone` and constructs
191-
* a new Application from the bindings provided by the `Promise` it returns.
219+
* # Usage
220+
*
221+
* `bindingFn` is a function that will be called in the new application's zone.
222+
* It should return a {@link Promise} to a list of bindings to be used for the
223+
* new application. Once this promise resolves, the application will be
224+
* constructed in the same manner as a normal `application()`.
192225
*/
193226
asyncApplication(bindingFn: (zone: NgZone) =>
194227
Promise<Array<Type | Binding | any[]>>): Promise<ApplicationRef> {
@@ -242,11 +275,9 @@ export class PlatformRef {
242275
}
243276

244277
/**
245-
* Represents an Angular application.
278+
* A reference to an Angular application running on a page.
246279
*
247-
* Use to retrieve the application {@link Injector} or to bootstrap new
248-
* components at the root of the application. Can also be used to dispose
249-
* of the entire application and all its loaded components.
280+
* For more about Angular applications, see the documentation for {@link bootstrap}.
250281
*/
251282
export class ApplicationRef {
252283
private _bootstrapListeners: Function[] = [];
@@ -258,15 +289,34 @@ export class ApplicationRef {
258289
constructor(private _platform: PlatformRef, private _zone: NgZone, private _injector: Injector) {}
259290

260291
/**
261-
* Register a listener to be called each time a new root component type is bootstrapped.
292+
* Register a listener to be called each time `bootstrap()` is called to bootstrap
293+
* a new root component.
262294
*/
263295
registerBootstrapListener(listener: (ref: ComponentRef) => void): void {
264296
this._bootstrapListeners.push(listener);
265297
}
266298

267299
/**
268-
* Bootstrap a new component at the root level of the application, optionally with
269-
* component specific bindings.
300+
* Bootstrap a new component at the root level of the application.
301+
*
302+
* # Bootstrap process
303+
*
304+
* When bootstrapping a new root component into an application, Angular mounts the
305+
* specified application component onto DOM elements identified by the [componentType]'s
306+
* selector and kicks off automatic change detection to finish initializing the component.
307+
*
308+
* # Optional Bindings
309+
*
310+
* Bindings for the given component can optionally be overridden via the `bindings`
311+
* parameter. These bindings will only apply for the root component being added and any
312+
* child components under it.
313+
*
314+
* # Example
315+
* ```
316+
* var app = platform.application([applicationCommonBindings(), applicationDomBindings()];
317+
* app.bootstrap(FirstRootComponent);
318+
* app.bootstrap(SecondRootComponent, [bind(OverrideBinding).toClass(OverriddenBinding)]);
319+
* ```
270320
*/
271321
bootstrap(componentType: Type, bindings?: Array<Type | Binding | any[]>): Promise<ComponentRef> {
272322
var completer = PromiseWrapper.completer();
@@ -312,6 +362,9 @@ export class ApplicationRef {
312362
*/
313363
get zone(): NgZone { return this._zone; }
314364

365+
/**
366+
* Dispose of this application and all of its components.
367+
*/
315368
dispose(): void {
316369
// TODO(alxhub): Dispose of the NgZone.
317370
this._rootComponents.forEach((ref) => ref.dispose());

modules/angular2/src/web_workers/worker/application_common.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,42 @@ import {WebWorkerEventDispatcher} from 'angular2/src/web_workers/worker/event_di
3636
import {ComponentRef} from 'angular2/src/core/compiler/dynamic_component_loader';
3737
import {NgZone} from 'angular2/src/core/zone/ng_zone';
3838

39+
/**
40+
* Initialize the Angular 'platform' on the page in a manner suitable for applications
41+
* running in a web worker. Applications running on a web worker do not have direct
42+
* access to DOM APIs.
43+
*
44+
* See {@link PlatformRef} for details on the Angular platform.
45+
*
46+
* # Without specified bindings
47+
*
48+
* If no bindings are specified, `platform`'s behavior depends on whether an existing
49+
* platform exists:
50+
*
51+
* If no platform exists, a new one will be created with the default {@link platformBindings}.
52+
*
53+
* If a platform already exists, it will be returned (regardless of what bindings it
54+
* was created with). This is a convenience feature, allowing for multiple applications
55+
* to be loaded into the same platform without awareness of each other.
56+
*
57+
* # With specified bindings
58+
*
59+
* It is also possible to specify bindings to be made in the new platform. These bindings
60+
* will be shared between all applications on the page. For example, an abstraction for
61+
* the browser cookie jar should be bound at the platform level, because there is only one
62+
* cookie jar regardless of how many applications on the age will be accessing it.
63+
*
64+
* If bindings are specified directly, `platform` will create the Angular platform with
65+
* them if a platform did not exist already. If it did exist, however, an error will be
66+
* thrown.
67+
*
68+
* # For Web Worker Appplications
69+
*
70+
* This version of `platform` initializes Angular for use with applications
71+
* that do not directly touch the DOM, such as applications which run in a
72+
* web worker context. Applications that need direct access to the DOM should
73+
* use `platform` from `core/application_common` instead.
74+
*/
3975
export function platform(bindings?: Array<Type | Binding | any[]>): PlatformRef {
4076
return platformCommon(bindings);
4177
}

0 commit comments

Comments
 (0)