From 4a9745ef782c0309783f97dac3cd6cafa5ac78a5 Mon Sep 17 00:00:00 2001 From: Chuck Jazdzewski Date: Mon, 15 Aug 2016 16:07:19 -0700 Subject: [PATCH] refactor(core): Remove deprecated DynamicComponentLoader (#10759) BREAKING CHANGE: previously deprecated DynamicComponentLoader was removed; see deprecation notice for migration instructions. --- .../@angular/core/src/application_module.ts | 2 - modules/@angular/core/src/linker.ts | 1 - .../src/linker/dynamic_component_loader.ts | 152 ---------- .../linker/dynamic_component_loader_spec.ts | 259 ------------------ tools/public_api_guard/core/index.d.ts | 6 - 5 files changed, 420 deletions(-) delete mode 100644 modules/@angular/core/src/linker/dynamic_component_loader.ts delete mode 100644 modules/@angular/core/test/linker/dynamic_component_loader_spec.ts diff --git a/modules/@angular/core/src/application_module.ts b/modules/@angular/core/src/application_module.ts index f6e28af62c..fbecfd5399 100644 --- a/modules/@angular/core/src/application_module.ts +++ b/modules/@angular/core/src/application_module.ts @@ -13,7 +13,6 @@ import {IterableDiffers, KeyValueDiffers, defaultIterableDiffers, defaultKeyValu import {LOCALE_ID} from './i18n/tokens'; import {Compiler} from './linker/compiler'; import {ComponentResolver} from './linker/component_resolver'; -import {DynamicComponentLoader, DynamicComponentLoader_} from './linker/dynamic_component_loader'; import {ViewUtils} from './linker/view_utils'; import {NgModule} from './metadata'; import {Type} from './type'; @@ -51,7 +50,6 @@ export const APPLICATION_COMMON_PROVIDERS: Array|{[k: string]: any}|an ViewUtils, {provide: IterableDiffers, useFactory: _iterableDiffersFactory}, {provide: KeyValueDiffers, useFactory: _keyValueDiffersFactory}, - {provide: DynamicComponentLoader, useClass: DynamicComponentLoader_}, {provide: LOCALE_ID, useValue: 'en_US'}, ] }) diff --git a/modules/@angular/core/src/linker.ts b/modules/@angular/core/src/linker.ts index 3cc5ebb1f5..642e7df076 100644 --- a/modules/@angular/core/src/linker.ts +++ b/modules/@angular/core/src/linker.ts @@ -11,7 +11,6 @@ export {COMPILER_OPTIONS, Compiler, CompilerFactory, CompilerOptions, ComponentS export {ComponentFactory, ComponentRef} from './linker/component_factory'; export {ComponentFactoryResolver, NoComponentFactoryError} from './linker/component_factory_resolver'; export {ComponentResolver} from './linker/component_resolver'; -export {DynamicComponentLoader} from './linker/dynamic_component_loader'; export {ElementRef} from './linker/element_ref'; export {ExpressionChangedAfterItHasBeenCheckedException} from './linker/exceptions'; export {NgModuleFactory, NgModuleRef} from './linker/ng_module_factory'; diff --git a/modules/@angular/core/src/linker/dynamic_component_loader.ts b/modules/@angular/core/src/linker/dynamic_component_loader.ts deleted file mode 100644 index fbe0d693ff..0000000000 --- a/modules/@angular/core/src/linker/dynamic_component_loader.ts +++ /dev/null @@ -1,152 +0,0 @@ -/** - * @license - * Copyright Google Inc. All Rights Reserved. - * - * Use of this source code is governed by an MIT-style license that can be - * found in the LICENSE file at https://angular.io/license - */ - -import {Injectable, Injector, ReflectiveInjector, ResolvedReflectiveProvider} from '../di'; -import {isPresent} from '../facade/lang'; -import {Type} from '../type'; -import {Compiler} from './compiler'; -import {ComponentRef} from './component_factory'; -import {ViewContainerRef} from './view_container_ref'; - - - -/** - * Use ComponentFactoryResolver and ViewContainerRef directly. - * - * @deprecated - */ -export abstract class DynamicComponentLoader { - /** - * Creates an instance of a Component `type` and attaches it to the first element in the - * platform-specific global view that matches the component's selector. - * - * In a browser the platform-specific global view is the main DOM Document. - * - * If needed, the component's selector can be overridden via `overrideSelector`. - * - * A provided {@link Injector} will be used to instantiate the Component. - * - * To be notified when this Component instance is destroyed, you can also optionally provide - * `onDispose` callback. - * - * Returns a promise for the {@link ComponentRef} representing the newly created Component. - * - * ### Example - * - * ``` - * @Component({ - * selector: 'child-component', - * template: 'Child' - * }) - * class ChildComponent { - * } - * - * @Component({ - * selector: 'my-app', - * template: 'Parent ()' - * }) - * class MyApp { - * constructor(dcl: DynamicComponentLoader, injector: Injector) { - * dcl.loadAsRoot(ChildComponent, '#child', injector); - * } - * } - * - * bootstrap(MyApp); - * ``` - * - * Resulting DOM: - * - * ``` - * - * Parent ( - * Child - * ) - * - * ``` - */ - abstract loadAsRoot( - type: Type, overrideSelectorOrNode: string|any, injector: Injector, - onDispose?: () => void, projectableNodes?: any[][]): Promise>; - - - /** - * Creates an instance of a Component and attaches it to the View Container found at the - * `location` specified as {@link ViewContainerRef}. - * - * You can optionally provide `providers` to configure the {@link Injector} provisioned for this - * Component Instance. - * - * Returns a promise for the {@link ComponentRef} representing the newly created Component. - * - * - * ### Example - * - * ``` - * @Component({ - * selector: 'child-component', - * template: 'Child' - * }) - * class ChildComponent { - * } - * - * @Component({ - * selector: 'my-app', - * template: 'Parent' - * }) - * class MyApp { - * constructor(dcl: DynamicComponentLoader, viewContainerRef: ViewContainerRef) { - * dcl.loadNextToLocation(ChildComponent, viewContainerRef); - * } - * } - * - * bootstrap(MyApp); - * ``` - * - * Resulting DOM: - * - * ``` - * Parent - * Child - * ``` - */ - abstract loadNextToLocation( - type: Type, location: ViewContainerRef, providers?: ResolvedReflectiveProvider[], - projectableNodes?: any[][]): Promise>; -} - -@Injectable() -export class DynamicComponentLoader_ extends DynamicComponentLoader { - constructor(private _compiler: Compiler) { super(); } - - loadAsRoot( - type: Type, overrideSelectorOrNode: string|any, injector: Injector, - onDispose?: () => void, projectableNodes?: any[][]): Promise> { - return this._compiler.compileComponentAsync(type).then(componentFactory => { - var componentRef = componentFactory.create( - injector, projectableNodes, - isPresent(overrideSelectorOrNode) ? overrideSelectorOrNode : componentFactory.selector); - if (isPresent(onDispose)) { - componentRef.onDestroy(onDispose); - } - return componentRef; - }); - } - - loadNextToLocation( - type: Type, location: ViewContainerRef, providers: ResolvedReflectiveProvider[] = null, - projectableNodes: any[][] = null): Promise> { - return this._compiler.compileComponentAsync(type).then(componentFactory => { - var contextInjector = location.parentInjector; - var childInjector = isPresent(providers) && providers.length > 0 ? - ReflectiveInjector.fromResolvedProviders(providers, contextInjector) : - contextInjector; - return location.createComponent( - componentFactory, location.length, childInjector, projectableNodes); - }); - } -} diff --git a/modules/@angular/core/test/linker/dynamic_component_loader_spec.ts b/modules/@angular/core/test/linker/dynamic_component_loader_spec.ts deleted file mode 100644 index 041e79f739..0000000000 --- a/modules/@angular/core/test/linker/dynamic_component_loader_spec.ts +++ /dev/null @@ -1,259 +0,0 @@ -/** - * @license - * Copyright Google Inc. All Rights Reserved. - * - * Use of this source code is governed by an MIT-style license that can be - * found in the LICENSE file at https://angular.io/license - */ - -import {DebugElement, Injector, Type, ViewChild, ViewContainerRef} from '@angular/core'; -import {DynamicComponentLoader} from '@angular/core/src/linker/dynamic_component_loader'; -import {ElementRef} from '@angular/core/src/linker/element_ref'; -import {Component} from '@angular/core/src/metadata'; -import {ComponentFixture, TestComponentBuilder} from '@angular/core/testing'; -import {AsyncTestCompleter, beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal'; -import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; -import {DOCUMENT} from '@angular/platform-browser/src/dom/dom_tokens'; -import {el} from '@angular/platform-browser/testing/browser_util'; -import {expect} from '@angular/platform-browser/testing/matchers'; - -import {Predicate} from '../../src/facade/collection'; -import {BaseException} from '../../src/facade/exceptions'; - -export function main() { - describe('DynamicComponentLoader', function() { - describe('loading next to a location', () => { - it('should work', - inject( - [DynamicComponentLoader, TestComponentBuilder, AsyncTestCompleter], - (loader: DynamicComponentLoader, tcb: TestComponentBuilder, - async: AsyncTestCompleter) => { - tcb.createAsync(MyComp3).then((tc) => { - tc.detectChanges(); - loader.loadNextToLocation(DynamicallyLoaded, tc.componentInstance.viewContainerRef) - .then(ref => { - expect(tc.debugElement.nativeElement).toHaveText('DynamicallyLoaded;'); - - async.done(); - }); - }); - })); - - it('should return a disposable component ref', - inject( - [DynamicComponentLoader, TestComponentBuilder, AsyncTestCompleter], - (loader: DynamicComponentLoader, tcb: TestComponentBuilder, - async: AsyncTestCompleter) => { - tcb.createAsync(MyComp3).then((tc) => { - tc.detectChanges(); - loader.loadNextToLocation(DynamicallyLoaded, tc.componentInstance.viewContainerRef) - .then(ref => { - loader - .loadNextToLocation( - DynamicallyLoaded2, tc.componentInstance.viewContainerRef) - .then(ref2 => { - expect(tc.debugElement.nativeElement) - .toHaveText('DynamicallyLoaded;DynamicallyLoaded2;'); - - ref2.destroy(); - - expect(tc.debugElement.nativeElement).toHaveText('DynamicallyLoaded;'); - - async.done(); - }); - }); - }); - })); - - it('should update host properties', - inject( - [DynamicComponentLoader, TestComponentBuilder, AsyncTestCompleter], - (loader: DynamicComponentLoader, tcb: TestComponentBuilder, - async: AsyncTestCompleter) => { - tcb.createAsync(MyComp3).then((tc) => { - tc.detectChanges(); - - loader - .loadNextToLocation( - DynamicallyLoadedWithHostProps, tc.componentInstance.viewContainerRef) - .then(ref => { - ref.instance.id = 'new value'; - - tc.detectChanges(); - - var newlyInsertedElement = tc.debugElement.childNodes[1].nativeNode; - expect((newlyInsertedElement).id).toEqual('new value'); - - async.done(); - }); - }); - })); - - - - it('should leave the view tree in a consistent state if hydration fails', - inject( - [DynamicComponentLoader, TestComponentBuilder, AsyncTestCompleter], - (loader: DynamicComponentLoader, tcb: TestComponentBuilder, - async: AsyncTestCompleter) => { - tcb.createAsync(MyComp3).then((tc: ComponentFixture) => { - tc.detectChanges(); - - loader - .loadNextToLocation( - DynamicallyLoadedThrows, tc.componentInstance.viewContainerRef) - .catch((error) => { - expect(error.message).toContain('ThrownInConstructor'); - expect(() => tc.detectChanges()).not.toThrow(); - async.done(); - return null; - }); - }); - })); - - it('should allow to pass projectable nodes', - inject( - [DynamicComponentLoader, TestComponentBuilder, AsyncTestCompleter], - (loader: DynamicComponentLoader, tcb: TestComponentBuilder, - async: AsyncTestCompleter) => { - tcb.createAsync(MyComp3).then((tc) => { - tc.detectChanges(); - loader - .loadNextToLocation( - DynamicallyLoadedWithNgContent, tc.componentInstance.viewContainerRef, - null, [[getDOM().createTextNode('hello')]]) - .then(ref => { - tc.detectChanges(); - var newlyInsertedElement = tc.debugElement.childNodes[1].nativeNode; - expect(newlyInsertedElement).toHaveText('dynamic(hello)'); - async.done(); - }); - }); - })); - - it('should not throw if not enough projectable nodes are passed in', - inject( - [DynamicComponentLoader, TestComponentBuilder, AsyncTestCompleter], - (loader: DynamicComponentLoader, tcb: TestComponentBuilder, - async: AsyncTestCompleter) => { - tcb.createAsync(MyComp3).then((tc) => { - tc.detectChanges(); - loader - .loadNextToLocation( - DynamicallyLoadedWithNgContent, tc.componentInstance.viewContainerRef, - null, []) - .then((_) => { async.done(); }); - }); - })); - - }); - - describe('loadAsRoot', () => { - it('should allow to create, update and destroy components', - inject( - [AsyncTestCompleter, DynamicComponentLoader, DOCUMENT, Injector], - (async: AsyncTestCompleter, loader: DynamicComponentLoader, doc: any /** TODO #9100 */, - injector: Injector) => { - var rootEl = createRootElement(doc, 'child-cmp'); - getDOM().appendChild(doc.body, rootEl); - loader.loadAsRoot(ChildComp, null, injector).then((componentRef) => { - var el = new ComponentFixture(componentRef, null, false); - - expect(rootEl.parentNode).toBe(doc.body); - - el.detectChanges(); - - expect(rootEl).toHaveText('hello'); - - componentRef.instance.ctxProp = 'new'; - - el.detectChanges(); - - expect(rootEl).toHaveText('new'); - - componentRef.destroy(); - - expect(rootEl.parentNode).toBeFalsy(); - - async.done(); - }); - })); - - it('should allow to pass projectable nodes', - inject( - [AsyncTestCompleter, DynamicComponentLoader, DOCUMENT, Injector], - (async: AsyncTestCompleter, loader: DynamicComponentLoader, doc: any /** TODO #9100 */, - injector: Injector) => { - var rootEl = createRootElement(doc, 'dummy'); - getDOM().appendChild(doc.body, rootEl); - loader - .loadAsRoot( - DynamicallyLoadedWithNgContent, null, injector, null, - [[getDOM().createTextNode('hello')]]) - .then((_) => { - expect(rootEl).toHaveText('dynamic(hello)'); - - async.done(); - }); - })); - - }); - - }); -} - -function createRootElement(doc: any, name: string): any { - var nodes = getDOM().querySelectorAll(doc, name); - for (var i = 0; i < nodes.length; i++) { - getDOM().remove(nodes[i]); - } - var rootEl = el(`<${name}>`); - getDOM().appendChild(doc.body, rootEl); - return rootEl; -} - -function filterByDirective(type: Type): Predicate { - return (debugElement) => { return debugElement.providerTokens.indexOf(type) !== -1; }; -} - -@Component({selector: 'child-cmp', template: '{{ctxProp}}'}) -class ChildComp { - ctxProp: string; - constructor(public elementRef: ElementRef) { this.ctxProp = 'hello'; } -} - -@Component({selector: 'dummy', template: 'DynamicallyLoaded;'}) -class DynamicallyLoaded { -} - -@Component({selector: 'dummy', template: 'DynamicallyLoaded;'}) -class DynamicallyLoadedThrows { - constructor() { throw new BaseException('ThrownInConstructor'); } -} - -@Component({selector: 'dummy', template: 'DynamicallyLoaded2;'}) -class DynamicallyLoaded2 { -} - -@Component({selector: 'dummy', host: {'[id]': 'id'}, template: 'DynamicallyLoadedWithHostProps;'}) -class DynamicallyLoadedWithHostProps { - id: string; - - constructor() { this.id = 'default'; } -} - -@Component({selector: 'dummy', template: 'dynamic()'}) -class DynamicallyLoadedWithNgContent { - id: string; - - constructor() { this.id = 'default'; } -} - -@Component({selector: 'my-comp', directives: [], template: '
'}) -class MyComp3 { - ctxBoolProp: boolean; - - @ViewChild('loc', {read: ViewContainerRef}) viewContainerRef: ViewContainerRef; - - constructor() { this.ctxBoolProp = false; } -} diff --git a/tools/public_api_guard/core/index.d.ts b/tools/public_api_guard/core/index.d.ts index efd00009b2..ea5be1293c 100644 --- a/tools/public_api_guard/core/index.d.ts +++ b/tools/public_api_guard/core/index.d.ts @@ -527,12 +527,6 @@ export declare abstract class DoCheck { abstract ngDoCheck(): void; } -/** @deprecated */ -export declare abstract class DynamicComponentLoader { - abstract loadAsRoot(type: Type, overrideSelectorOrNode: string | any, injector: Injector, onDispose?: () => void, projectableNodes?: any[][]): Promise>; - abstract loadNextToLocation(type: Type, location: ViewContainerRef, providers?: ResolvedReflectiveProvider[], projectableNodes?: any[][]): Promise>; -} - /** @stable */ export declare class ElementRef { /** @stable */ nativeElement: any;