2015-10-08 16:33:22 -04:00
|
|
|
import {NgZone} from 'angular2/src/core/zone/ng_zone';
|
2015-09-02 18:19:26 -04:00
|
|
|
import {Type, isBlank, isPresent, assertionsEnabled} from 'angular2/src/core/facade/lang';
|
2015-10-11 01:11:13 -04:00
|
|
|
import {provide, Provider, Injector, OpaqueToken} from 'angular2/src/core/di';
|
2015-10-02 12:53:57 -04:00
|
|
|
import {
|
|
|
|
APP_COMPONENT_REF_PROMISE,
|
|
|
|
APP_COMPONENT,
|
2015-10-11 01:11:13 -04:00
|
|
|
APP_ID_RANDOM_PROVIDER
|
2015-10-02 12:53:57 -04:00
|
|
|
} from './application_tokens';
|
2015-09-02 18:19:26 -04:00
|
|
|
import {Promise, PromiseWrapper, PromiseCompleter} from 'angular2/src/core/facade/async';
|
|
|
|
import {ListWrapper} from 'angular2/src/core/facade/collection';
|
|
|
|
import {Reflector, reflector} from 'angular2/src/core/reflection/reflection';
|
|
|
|
import {TestabilityRegistry, Testability} from 'angular2/src/core/testability/testability';
|
|
|
|
import {
|
|
|
|
ComponentRef,
|
|
|
|
DynamicComponentLoader
|
2015-10-02 10:37:23 -04:00
|
|
|
} from 'angular2/src/core/linker/dynamic_component_loader';
|
2015-09-02 18:19:26 -04:00
|
|
|
import {
|
|
|
|
BaseException,
|
|
|
|
WrappedException,
|
2015-10-06 09:53:39 -04:00
|
|
|
ExceptionHandler,
|
|
|
|
unimplemented
|
2015-09-02 18:19:26 -04:00
|
|
|
} from 'angular2/src/core/facade/exceptions';
|
|
|
|
import {DOM} from 'angular2/src/core/dom/dom_adapter';
|
2015-10-02 10:37:23 -04:00
|
|
|
import {internalView} from 'angular2/src/core/linker/view_ref';
|
2015-10-06 09:53:39 -04:00
|
|
|
import {LifeCycle, LifeCycle_} from 'angular2/src/core/life_cycle/life_cycle';
|
2015-09-02 18:19:26 -04:00
|
|
|
import {
|
|
|
|
IterableDiffers,
|
|
|
|
defaultIterableDiffers,
|
|
|
|
KeyValueDiffers,
|
|
|
|
defaultKeyValueDiffers
|
|
|
|
} from 'angular2/src/core/change_detection/change_detection';
|
2015-10-02 10:37:23 -04:00
|
|
|
import {AppViewPool, APP_VIEW_POOL_CAPACITY} from 'angular2/src/core/linker/view_pool';
|
|
|
|
import {AppViewManager} from 'angular2/src/core/linker/view_manager';
|
|
|
|
import {AppViewManagerUtils} from 'angular2/src/core/linker/view_manager_utils';
|
|
|
|
import {AppViewListener} from 'angular2/src/core/linker/view_listener';
|
|
|
|
import {ProtoViewFactory} from './linker/proto_view_factory';
|
2015-09-02 18:19:26 -04:00
|
|
|
import {DEFAULT_PIPES} from 'angular2/src/core/pipes';
|
2015-10-02 10:37:23 -04:00
|
|
|
import {ViewResolver} from './linker/view_resolver';
|
|
|
|
import {DirectiveResolver} from './linker/directive_resolver';
|
|
|
|
import {PipeResolver} from './linker/pipe_resolver';
|
|
|
|
import {Compiler} from 'angular2/src/core/linker/compiler';
|
2015-10-06 22:39:44 -04:00
|
|
|
import {DynamicComponentLoader_} from "./linker/dynamic_component_loader";
|
|
|
|
import {AppViewManager_} from "./linker/view_manager";
|
|
|
|
import {Compiler_} from "./linker/compiler";
|
2015-09-02 18:19:26 -04:00
|
|
|
|
|
|
|
/**
|
2015-10-11 01:11:13 -04:00
|
|
|
* Constructs the set of providers meant for use at the platform level.
|
2015-09-15 18:51:15 -04:00
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* These are providers that should be singletons shared among all Angular applications
|
2015-09-15 18:51:15 -04:00
|
|
|
* running on the page.
|
2015-09-02 18:19:26 -04:00
|
|
|
*/
|
2015-10-19 22:05:39 -04:00
|
|
|
export function platformProviders(): Array<Type | Provider | any[]> {
|
2015-10-12 14:30:34 -04:00
|
|
|
return [provide(Reflector, {useValue: reflector}), TestabilityRegistry];
|
2015-09-02 18:19:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-11 01:11:13 -04:00
|
|
|
* Construct providers specific to an individual root component.
|
2015-09-02 18:19:26 -04:00
|
|
|
*/
|
2015-10-11 01:11:13 -04:00
|
|
|
function _componentProviders(appComponentType: Type): Array<Type | Provider | any[]> {
|
2015-09-02 18:19:26 -04:00
|
|
|
return [
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(APP_COMPONENT, {useValue: appComponentType}),
|
2015-10-11 01:11:13 -04:00
|
|
|
provide(APP_COMPONENT_REF_PROMISE,
|
|
|
|
{
|
2015-10-12 14:30:34 -04:00
|
|
|
useFactory: (dynamicComponentLoader, injector: Injector) => {
|
2015-10-19 22:05:39 -04:00
|
|
|
// TODO(rado): investigate whether to support providers on root component.
|
2015-10-11 01:11:13 -04:00
|
|
|
return dynamicComponentLoader.loadAsRoot(appComponentType, null, injector)
|
|
|
|
.then((componentRef) => {
|
|
|
|
if (isPresent(componentRef.location.nativeElement)) {
|
|
|
|
injector.get(TestabilityRegistry)
|
|
|
|
.registerApplication(componentRef.location.nativeElement,
|
|
|
|
injector.get(Testability));
|
|
|
|
}
|
|
|
|
return componentRef;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
deps: [DynamicComponentLoader, Injector]
|
|
|
|
}),
|
|
|
|
provide(appComponentType,
|
|
|
|
{
|
2015-10-12 14:30:34 -04:00
|
|
|
useFactory: (p: Promise<any>) => p.then(ref => ref.instance),
|
2015-10-11 01:11:13 -04:00
|
|
|
deps: [APP_COMPONENT_REF_PROMISE]
|
|
|
|
}),
|
2015-09-02 18:19:26 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-11 01:11:13 -04:00
|
|
|
* Construct a default set of providers which should be included in any Angular
|
2015-09-02 18:19:26 -04:00
|
|
|
* application, regardless of whether it runs on the UI thread or in a web worker.
|
|
|
|
*/
|
2015-10-19 22:05:39 -04:00
|
|
|
export function applicationCommonProviders(): Array<Type | Provider | any[]> {
|
2015-09-02 18:19:26 -04:00
|
|
|
return [
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(Compiler, {useClass: Compiler_}),
|
2015-10-11 01:11:13 -04:00
|
|
|
APP_ID_RANDOM_PROVIDER,
|
2015-09-02 18:19:26 -04:00
|
|
|
AppViewPool,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(APP_VIEW_POOL_CAPACITY, {useValue: 10000}),
|
|
|
|
provide(AppViewManager, {useClass: AppViewManager_}),
|
2015-09-02 18:19:26 -04:00
|
|
|
AppViewManagerUtils,
|
|
|
|
AppViewListener,
|
2015-10-01 13:07:49 -04:00
|
|
|
ProtoViewFactory,
|
2015-09-02 18:19:26 -04:00
|
|
|
ViewResolver,
|
|
|
|
DEFAULT_PIPES,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(IterableDiffers, {useValue: defaultIterableDiffers}),
|
|
|
|
provide(KeyValueDiffers, {useValue: defaultKeyValueDiffers}),
|
2015-09-02 18:19:26 -04:00
|
|
|
DirectiveResolver,
|
|
|
|
PipeResolver,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(DynamicComponentLoader, {useClass: DynamicComponentLoader_}),
|
2015-10-11 01:11:13 -04:00
|
|
|
provide(LifeCycle,
|
|
|
|
{
|
2015-10-12 14:30:34 -04:00
|
|
|
useFactory: (exceptionHandler) => new LifeCycle_(null, assertionsEnabled()),
|
2015-10-11 01:11:13 -04:00
|
|
|
deps: [ExceptionHandler]
|
|
|
|
})
|
2015-09-02 18:19:26 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an Angular zone.
|
|
|
|
*/
|
|
|
|
export function createNgZone(): NgZone {
|
2015-10-08 16:33:22 -04:00
|
|
|
return new NgZone({enableLongStackTrace: assertionsEnabled()});
|
2015-09-02 18:19:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var _platform: PlatformRef;
|
|
|
|
|
2015-10-19 22:05:39 -04:00
|
|
|
export function platformCommon(providers?: Array<Type | Provider | any[]>,
|
|
|
|
initializer?: () => void): PlatformRef {
|
2015-09-02 18:19:26 -04:00
|
|
|
if (isPresent(_platform)) {
|
2015-10-19 22:05:39 -04:00
|
|
|
if (isBlank(providers)) {
|
2015-09-02 18:19:26 -04:00
|
|
|
return _platform;
|
|
|
|
}
|
|
|
|
throw "platform() can only be called once per page";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isPresent(initializer)) {
|
|
|
|
initializer();
|
|
|
|
}
|
|
|
|
|
2015-10-19 22:05:39 -04:00
|
|
|
if (isBlank(providers)) {
|
|
|
|
providers = platformProviders();
|
2015-09-02 18:19:26 -04:00
|
|
|
}
|
2015-10-19 22:05:39 -04:00
|
|
|
_platform = new PlatformRef_(Injector.resolveAndCreate(providers), () => { _platform = null; });
|
2015-09-02 18:19:26 -04:00
|
|
|
return _platform;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-15 18:51:15 -04:00
|
|
|
* The Angular platform is the entry point for Angular on a web page. Each page
|
|
|
|
* has exactly one platform, and services (such as reflection) which are common
|
|
|
|
* to every Angular application running on the page are bound in its scope.
|
2015-08-20 20:18:27 -04:00
|
|
|
*
|
2015-09-15 18:51:15 -04:00
|
|
|
* A page's platform is initialized implicitly when {@link bootstrap}() is called, or
|
|
|
|
* explicitly by calling {@link platform}().
|
2015-08-20 20:18:27 -04:00
|
|
|
*/
|
2015-10-06 09:53:39 -04:00
|
|
|
export abstract class PlatformRef {
|
2015-10-26 13:50:25 -04:00
|
|
|
/**
|
|
|
|
* Register a listener to be called when the platform is disposed.
|
|
|
|
*/
|
|
|
|
abstract registerDisposeListener(dispose: () => void): void;
|
|
|
|
|
2015-09-02 18:19:26 -04:00
|
|
|
/**
|
2015-09-15 18:51:15 -04:00
|
|
|
* Retrieve the platform {@link Injector}, which is the parent injector for
|
2015-10-11 01:11:13 -04:00
|
|
|
* every Angular application on the page and provides singleton providers.
|
2015-09-02 18:19:26 -04:00
|
|
|
*/
|
2015-10-06 09:53:39 -04:00
|
|
|
get injector(): Injector { return unimplemented(); };
|
2015-08-20 20:18:27 -04:00
|
|
|
|
|
|
|
/**
|
2015-09-15 18:51:15 -04:00
|
|
|
* Instantiate a new Angular application on the page.
|
|
|
|
*
|
2015-10-19 10:37:32 -04:00
|
|
|
*##What is an application?
|
2015-09-15 18:51:15 -04:00
|
|
|
*
|
|
|
|
* Each Angular application has its own zone, change detection, compiler,
|
|
|
|
* renderer, and other framework components. An application hosts one or more
|
|
|
|
* root components, which can be initialized via `ApplicationRef.bootstrap()`.
|
|
|
|
*
|
2015-10-19 22:05:39 -04:00
|
|
|
*##Application Providers
|
2015-09-15 18:51:15 -04:00
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* Angular applications require numerous providers to be properly instantiated.
|
|
|
|
* When using `application()` to create a new app on the page, these providers
|
2015-09-15 18:51:15 -04:00
|
|
|
* must be provided. Fortunately, there are helper functions to configure
|
2015-10-11 01:11:13 -04:00
|
|
|
* typical providers, as shown in the example below.
|
2015-09-15 18:51:15 -04:00
|
|
|
*
|
2015-10-19 10:37:32 -04:00
|
|
|
* ### Example
|
2015-09-15 18:51:15 -04:00
|
|
|
* ```
|
2015-10-19 22:05:39 -04:00
|
|
|
* var myAppProviders = [MyAppService];
|
2015-09-15 18:51:15 -04:00
|
|
|
*
|
|
|
|
* platform()
|
2015-10-19 22:05:39 -04:00
|
|
|
* .application([applicationCommonProviders(), applicationDomProviders(), myAppProviders])
|
2015-09-15 18:51:15 -04:00
|
|
|
* .bootstrap(MyTopLevelComponent);
|
|
|
|
* ```
|
2015-10-19 10:37:32 -04:00
|
|
|
*##See Also
|
2015-09-15 18:51:15 -04:00
|
|
|
*
|
|
|
|
* See the {@link bootstrap} documentation for more details.
|
2015-08-20 20:18:27 -04:00
|
|
|
*/
|
2015-10-19 22:05:39 -04:00
|
|
|
abstract application(providers: Array<Type | Provider | any[]>): ApplicationRef;
|
2015-08-20 20:18:27 -04:00
|
|
|
|
|
|
|
/**
|
2015-10-11 01:11:13 -04:00
|
|
|
* Instantiate a new Angular application on the page, using providers which
|
2015-09-15 18:51:15 -04:00
|
|
|
* are only available asynchronously. One such use case is to initialize an
|
|
|
|
* application running in a web worker.
|
2015-09-02 18:19:26 -04:00
|
|
|
*
|
2015-10-19 10:37:32 -04:00
|
|
|
*##Usage
|
2015-09-15 18:51:15 -04:00
|
|
|
*
|
|
|
|
* `bindingFn` is a function that will be called in the new application's zone.
|
2015-10-13 03:06:39 -04:00
|
|
|
* It should return a `Promise` to a list of providers to be used for the
|
2015-09-15 18:51:15 -04:00
|
|
|
* new application. Once this promise resolves, the application will be
|
|
|
|
* constructed in the same manner as a normal `application()`.
|
2015-08-20 20:18:27 -04:00
|
|
|
*/
|
2015-10-28 03:59:19 -04:00
|
|
|
abstract asyncApplication(bindingFn: (zone: NgZone) =>
|
|
|
|
Promise<Array<Type | Provider | any[]>>): Promise<ApplicationRef>;
|
2015-10-06 09:53:39 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the Angular platform and all Angular applications on the page.
|
|
|
|
*/
|
|
|
|
abstract dispose(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PlatformRef_ extends PlatformRef {
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-10-06 09:53:39 -04:00
|
|
|
_applications: ApplicationRef[] = [];
|
2015-10-26 13:50:25 -04:00
|
|
|
_disposeListeners: Function[] = [];
|
2015-10-06 09:53:39 -04:00
|
|
|
|
|
|
|
constructor(private _injector: Injector, private _dispose: () => void) { super(); }
|
|
|
|
|
2015-10-26 13:50:25 -04:00
|
|
|
registerDisposeListener(dispose: () => void): void { this._disposeListeners.push(dispose); }
|
|
|
|
|
2015-10-06 09:53:39 -04:00
|
|
|
get injector(): Injector { return this._injector; }
|
|
|
|
|
2015-10-19 22:05:39 -04:00
|
|
|
application(providers: Array<Type | Provider | any[]>): ApplicationRef {
|
|
|
|
var app = this._initApp(createNgZone(), providers);
|
2015-10-06 09:53:39 -04:00
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2015-10-28 03:59:19 -04:00
|
|
|
asyncApplication(bindingFn: (zone: NgZone) => Promise<Array<Type | Provider | any[]>>):
|
|
|
|
Promise<ApplicationRef> {
|
2015-09-02 18:19:26 -04:00
|
|
|
var zone = createNgZone();
|
|
|
|
var completer = PromiseWrapper.completer();
|
|
|
|
zone.run(() => {
|
2015-10-19 22:05:39 -04:00
|
|
|
PromiseWrapper.then(bindingFn(zone), (providers: Array<Type | Provider | any[]>) => {
|
|
|
|
completer.resolve(this._initApp(zone, providers));
|
2015-09-02 18:19:26 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return completer.promise;
|
|
|
|
}
|
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
private _initApp(zone: NgZone, providers: Array<Type | Provider | any[]>): ApplicationRef {
|
2015-09-02 18:19:26 -04:00
|
|
|
var injector: Injector;
|
2015-10-09 19:22:07 -04:00
|
|
|
var app: ApplicationRef;
|
2015-09-02 18:19:26 -04:00
|
|
|
zone.run(() => {
|
2015-10-12 14:30:34 -04:00
|
|
|
providers.push(provide(NgZone, {useValue: zone}));
|
|
|
|
providers.push(provide(ApplicationRef, {useFactory: (): ApplicationRef => app, deps: []}));
|
2015-09-02 18:19:26 -04:00
|
|
|
|
|
|
|
var exceptionHandler;
|
|
|
|
try {
|
2015-10-11 01:11:13 -04:00
|
|
|
injector = this.injector.resolveAndCreateChild(providers);
|
2015-09-02 18:19:26 -04:00
|
|
|
exceptionHandler = injector.get(ExceptionHandler);
|
2015-10-08 16:33:22 -04:00
|
|
|
zone.overrideOnErrorHandler((e, s) => exceptionHandler.call(e, s));
|
2015-09-02 18:19:26 -04:00
|
|
|
} catch (e) {
|
|
|
|
if (isPresent(exceptionHandler)) {
|
|
|
|
exceptionHandler.call(e, e.stack);
|
|
|
|
} else {
|
|
|
|
DOM.logError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-10-09 19:22:07 -04:00
|
|
|
app = new ApplicationRef_(this, zone, injector);
|
2015-09-02 18:19:26 -04:00
|
|
|
this._applications.push(app);
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2015-08-20 20:18:27 -04:00
|
|
|
dispose(): void {
|
2015-09-02 18:19:26 -04:00
|
|
|
this._applications.forEach((app) => app.dispose());
|
2015-10-26 13:50:25 -04:00
|
|
|
this._disposeListeners.forEach((dispose) => dispose());
|
2015-09-02 18:19:26 -04:00
|
|
|
this._dispose();
|
|
|
|
}
|
|
|
|
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-09-02 18:19:26 -04:00
|
|
|
_applicationDisposed(app: ApplicationRef): void { ListWrapper.remove(this._applications, app); }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-15 18:51:15 -04:00
|
|
|
* A reference to an Angular application running on a page.
|
2015-09-02 18:19:26 -04:00
|
|
|
*
|
2015-09-15 18:51:15 -04:00
|
|
|
* For more about Angular applications, see the documentation for {@link bootstrap}.
|
2015-09-02 18:19:26 -04:00
|
|
|
*/
|
2015-10-06 09:53:39 -04:00
|
|
|
export abstract class ApplicationRef {
|
2015-09-02 18:19:26 -04:00
|
|
|
/**
|
2015-09-15 18:51:15 -04:00
|
|
|
* Register a listener to be called each time `bootstrap()` is called to bootstrap
|
|
|
|
* a new root component.
|
2015-09-02 18:19:26 -04:00
|
|
|
*/
|
2015-10-06 09:53:39 -04:00
|
|
|
abstract registerBootstrapListener(listener: (ref: ComponentRef) => void): void;
|
2015-08-20 20:18:27 -04:00
|
|
|
|
2015-10-26 13:50:25 -04:00
|
|
|
/**
|
|
|
|
* Register a listener to be called when the application is disposed.
|
|
|
|
*/
|
|
|
|
abstract registerDisposeListener(dispose: () => void): void;
|
|
|
|
|
2015-08-20 20:18:27 -04:00
|
|
|
/**
|
2015-09-15 18:51:15 -04:00
|
|
|
* Bootstrap a new component at the root level of the application.
|
|
|
|
*
|
2015-10-19 10:37:32 -04:00
|
|
|
*##Bootstrap process
|
2015-09-15 18:51:15 -04:00
|
|
|
*
|
|
|
|
* When bootstrapping a new root component into an application, Angular mounts the
|
|
|
|
* specified application component onto DOM elements identified by the [componentType]'s
|
|
|
|
* selector and kicks off automatic change detection to finish initializing the component.
|
|
|
|
*
|
2015-10-19 22:05:39 -04:00
|
|
|
*##Optional Providers
|
2015-09-15 18:51:15 -04:00
|
|
|
*
|
2015-10-19 22:05:39 -04:00
|
|
|
* Providers for the given component can optionally be overridden via the `providers`
|
2015-10-11 01:11:13 -04:00
|
|
|
* parameter. These providers will only apply for the root component being added and any
|
2015-09-15 18:51:15 -04:00
|
|
|
* child components under it.
|
|
|
|
*
|
2015-10-19 10:37:32 -04:00
|
|
|
* ### Example
|
2015-09-15 18:51:15 -04:00
|
|
|
* ```
|
2015-10-19 22:05:39 -04:00
|
|
|
* var app = platform.application([applicationCommonProviders(), applicationDomProviders()];
|
2015-09-15 18:51:15 -04:00
|
|
|
* app.bootstrap(FirstRootComponent);
|
2015-10-12 14:30:34 -04:00
|
|
|
* app.bootstrap(SecondRootComponent, [provide(OverrideBinding, {useClass: OverriddenBinding})]);
|
2015-09-15 18:51:15 -04:00
|
|
|
* ```
|
2015-09-02 18:19:26 -04:00
|
|
|
*/
|
2015-10-28 03:59:19 -04:00
|
|
|
abstract bootstrap(componentType: Type,
|
|
|
|
providers?: Array<Type | Provider | any[]>): Promise<ComponentRef>;
|
2015-10-06 09:53:39 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the application {@link Injector}.
|
|
|
|
*/
|
|
|
|
get injector(): Injector { return unimplemented(); };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the application {@link NgZone}.
|
|
|
|
*/
|
|
|
|
get zone(): NgZone { return unimplemented(); };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispose of this application and all of its components.
|
|
|
|
*/
|
|
|
|
abstract dispose(): void;
|
2015-10-09 19:22:07 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of component types registered to this application.
|
|
|
|
*/
|
|
|
|
get componentTypes(): Type[] { return unimplemented(); };
|
2015-10-06 09:53:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ApplicationRef_ extends ApplicationRef {
|
|
|
|
private _bootstrapListeners: Function[] = [];
|
2015-10-26 13:50:25 -04:00
|
|
|
private _disposeListeners: Function[] = [];
|
2015-10-06 09:53:39 -04:00
|
|
|
private _rootComponents: ComponentRef[] = [];
|
2015-10-09 19:22:07 -04:00
|
|
|
private _rootComponentTypes: Type[] = [];
|
2015-10-06 09:53:39 -04:00
|
|
|
|
|
|
|
constructor(private _platform: PlatformRef_, private _zone: NgZone, private _injector: Injector) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
registerBootstrapListener(listener: (ref: ComponentRef) => void): void {
|
|
|
|
this._bootstrapListeners.push(listener);
|
|
|
|
}
|
|
|
|
|
2015-10-26 13:50:25 -04:00
|
|
|
registerDisposeListener(dispose: () => void): void { this._disposeListeners.push(dispose); }
|
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
bootstrap(componentType: Type,
|
|
|
|
providers?: Array<Type | Provider | any[]>): Promise<ComponentRef> {
|
2015-09-02 18:19:26 -04:00
|
|
|
var completer = PromiseWrapper.completer();
|
|
|
|
this._zone.run(() => {
|
2015-10-11 01:11:13 -04:00
|
|
|
var componentProviders = _componentProviders(componentType);
|
|
|
|
if (isPresent(providers)) {
|
|
|
|
componentProviders.push(providers);
|
2015-09-02 18:19:26 -04:00
|
|
|
}
|
|
|
|
var exceptionHandler = this._injector.get(ExceptionHandler);
|
2015-10-09 19:22:07 -04:00
|
|
|
this._rootComponentTypes.push(componentType);
|
2015-09-02 18:19:26 -04:00
|
|
|
try {
|
2015-10-11 01:11:13 -04:00
|
|
|
var injector: Injector = this._injector.resolveAndCreateChild(componentProviders);
|
2015-09-02 18:19:26 -04:00
|
|
|
var compRefToken: Promise<ComponentRef> = injector.get(APP_COMPONENT_REF_PROMISE);
|
|
|
|
var tick = (componentRef) => {
|
|
|
|
var appChangeDetector = internalView(componentRef.hostView).changeDetector;
|
|
|
|
var lc = injector.get(LifeCycle);
|
|
|
|
lc.registerWith(this._zone, appChangeDetector);
|
|
|
|
lc.tick();
|
|
|
|
completer.resolve(componentRef);
|
|
|
|
this._rootComponents.push(componentRef);
|
|
|
|
this._bootstrapListeners.forEach((listener) => listener(componentRef));
|
|
|
|
};
|
|
|
|
|
|
|
|
var tickResult = PromiseWrapper.then(compRefToken, tick);
|
|
|
|
|
|
|
|
PromiseWrapper.then(tickResult, (_) => {});
|
|
|
|
PromiseWrapper.then(tickResult, null,
|
|
|
|
(err, stackTrace) => completer.reject(err, stackTrace));
|
|
|
|
} catch (e) {
|
|
|
|
exceptionHandler.call(e, e.stack);
|
|
|
|
completer.reject(e, e.stack);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return completer.promise;
|
|
|
|
}
|
|
|
|
|
2015-08-20 20:18:27 -04:00
|
|
|
get injector(): Injector { return this._injector; }
|
2015-09-02 18:19:26 -04:00
|
|
|
|
|
|
|
get zone(): NgZone { return this._zone; }
|
|
|
|
|
|
|
|
dispose(): void {
|
|
|
|
// TODO(alxhub): Dispose of the NgZone.
|
|
|
|
this._rootComponents.forEach((ref) => ref.dispose());
|
2015-10-26 13:50:25 -04:00
|
|
|
this._disposeListeners.forEach((dispose) => dispose());
|
2015-09-02 18:19:26 -04:00
|
|
|
this._platform._applicationDisposed(this);
|
|
|
|
}
|
2015-10-09 19:22:07 -04:00
|
|
|
|
|
|
|
get componentTypes(): any[] { return this._rootComponentTypes; }
|
2015-08-20 20:18:27 -04:00
|
|
|
}
|