diff --git a/modules/angular2/src/core/application_ref.ts b/modules/angular2/src/core/application_ref.ts index c669ceaa56..8946c2f941 100644 --- a/modules/angular2/src/core/application_ref.ts +++ b/modules/angular2/src/core/application_ref.ts @@ -150,6 +150,11 @@ export function platformCommon(bindings?: Array, initia * explicitly by calling {@link platform}(). */ export abstract class PlatformRef { + /** + * Register a listener to be called when the platform is disposed. + */ + abstract registerDisposeListener(dispose: () => void): void; + /** * Retrieve the platform {@link Injector}, which is the parent injector for * every Angular application on the page and provides singleton providers. @@ -210,9 +215,12 @@ export abstract class PlatformRef { export class PlatformRef_ extends PlatformRef { /** @internal */ _applications: ApplicationRef[] = []; + _disposeListeners: Function[] = []; constructor(private _injector: Injector, private _dispose: () => void) { super(); } + registerDisposeListener(dispose: () => void): void { this._disposeListeners.push(dispose); } + get injector(): Injector { return this._injector; } application(bindings: Array): ApplicationRef { @@ -259,6 +267,7 @@ export class PlatformRef_ extends PlatformRef { dispose(): void { this._applications.forEach((app) => app.dispose()); + this._disposeListeners.forEach((dispose) => dispose()); this._dispose(); } @@ -278,6 +287,11 @@ export abstract class ApplicationRef { */ abstract registerBootstrapListener(listener: (ref: ComponentRef) => void): void; + /** + * Register a listener to be called when the application is disposed. + */ + abstract registerDisposeListener(dispose: () => void): void; + /** * Bootstrap a new component at the root level of the application. * @@ -326,6 +340,7 @@ export abstract class ApplicationRef { export class ApplicationRef_ extends ApplicationRef { private _bootstrapListeners: Function[] = []; + private _disposeListeners: Function[] = []; private _rootComponents: ComponentRef[] = []; private _rootComponentTypes: Type[] = []; @@ -337,6 +352,8 @@ export class ApplicationRef_ extends ApplicationRef { this._bootstrapListeners.push(listener); } + registerDisposeListener(dispose: () => void): void { this._disposeListeners.push(dispose); } + bootstrap(componentType: Type, providers?: Array): Promise { var completer = PromiseWrapper.completer(); @@ -380,6 +397,7 @@ export class ApplicationRef_ extends ApplicationRef { dispose(): void { // TODO(alxhub): Dispose of the NgZone. this._rootComponents.forEach((ref) => ref.dispose()); + this._disposeListeners.forEach((dispose) => dispose()); this._platform._applicationDisposed(this); } diff --git a/modules/angular2/src/mock/mock_application_ref.ts b/modules/angular2/src/mock/mock_application_ref.ts new file mode 100644 index 0000000000..b683767d27 --- /dev/null +++ b/modules/angular2/src/mock/mock_application_ref.ts @@ -0,0 +1,24 @@ +import {ApplicationRef} from 'angular2/src/core/application_ref'; +import {Type} from 'angular2/src/core/facade/lang'; +import {ComponentRef} from 'angular2/src/core/linker/dynamic_component_loader'; +import {Provider, Injector} from 'angular2/src/core/di'; +import {NgZone} from 'angular2/src/core/zone/ng_zone'; +import {Promise} from 'angular2/src/core/facade/async'; + +export class MockApplicationRef extends ApplicationRef { + registerBootstrapListener(listener: (ref: ComponentRef) => void): void {} + + registerDisposeListener(dispose: () => void): void {} + + bootstrap(componentType: Type, bindings?: Array): Promise { + return null; + } + + get injector(): Injector { return null; }; + + get zone(): NgZone { return null; }; + + dispose(): void {} + + get componentTypes(): Type[] { return null; }; +} \ No newline at end of file diff --git a/modules/angular2/test/public_api_spec.ts b/modules/angular2/test/public_api_spec.ts index af640687c5..c287a70e4a 100644 --- a/modules/angular2/test/public_api_spec.ts +++ b/modules/angular2/test/public_api_spec.ts @@ -98,6 +98,7 @@ var NG_API = [ 'ApplicationRef.bootstrap()', 'ApplicationRef.dispose()', 'ApplicationRef.registerBootstrapListener()', + 'ApplicationRef.registerDisposeListener()', */ 'AsyncPipe', 'AsyncPipe.onDestroy()', @@ -873,6 +874,7 @@ var NG_API = [ 'PlatformRef.application()', 'PlatformRef.asyncApplication()', 'PlatformRef.dispose()', + 'PlatformRef.registerDisposeListener()', */ 'PlatformRef.injector', 'Predicate:dart',