refactor(core): Remove deprecated DynamicComponentLoader (#10759)

BREAKING CHANGE: previously deprecated DynamicComponentLoader was removed; see deprecation notice for migration instructions.
This commit is contained in:
Chuck Jazdzewski 2016-08-15 16:07:19 -07:00 committed by vikerman
parent acc0fe6cf9
commit 4a9745ef78
5 changed files with 0 additions and 420 deletions

View File

@ -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<Type<any>|{[k: string]: any}|an
ViewUtils,
{provide: IterableDiffers, useFactory: _iterableDiffersFactory},
{provide: KeyValueDiffers, useFactory: _keyValueDiffersFactory},
{provide: DynamicComponentLoader, useClass: DynamicComponentLoader_},
{provide: LOCALE_ID, useValue: 'en_US'},
]
})

View File

@ -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';

View File

@ -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 (<child id="child"></child>)'
* })
* class MyApp {
* constructor(dcl: DynamicComponentLoader, injector: Injector) {
* dcl.loadAsRoot(ChildComponent, '#child', injector);
* }
* }
*
* bootstrap(MyApp);
* ```
*
* Resulting DOM:
*
* ```
* <my-app>
* Parent (
* <child id="child">Child</child>
* )
* </my-app>
* ```
*/
abstract loadAsRoot(
type: Type<any>, overrideSelectorOrNode: string|any, injector: Injector,
onDispose?: () => void, projectableNodes?: any[][]): Promise<ComponentRef<any>>;
/**
* 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:
*
* ```
* <my-app>Parent</my-app>
* <child-component>Child</child-component>
* ```
*/
abstract loadNextToLocation(
type: Type<any>, location: ViewContainerRef, providers?: ResolvedReflectiveProvider[],
projectableNodes?: any[][]): Promise<ComponentRef<any>>;
}
@Injectable()
export class DynamicComponentLoader_ extends DynamicComponentLoader {
constructor(private _compiler: Compiler) { super(); }
loadAsRoot(
type: Type<any>, overrideSelectorOrNode: string|any, injector: Injector,
onDispose?: () => void, projectableNodes?: any[][]): Promise<ComponentRef<any>> {
return this._compiler.compileComponentAsync(<any>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<any>, location: ViewContainerRef, providers: ResolvedReflectiveProvider[] = null,
projectableNodes: any[][] = null): Promise<ComponentRef<any>> {
return this._compiler.compileComponentAsync(<any>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);
});
}
}

View File

@ -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((<HTMLElement>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<any>) => {
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<any>(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}></${name}>`);
getDOM().appendChild(doc.body, rootEl);
return rootEl;
}
function filterByDirective(type: Type<any>): Predicate<DebugElement> {
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(<ng-content></ng-content>)'})
class DynamicallyLoadedWithNgContent {
id: string;
constructor() { this.id = 'default'; }
}
@Component({selector: 'my-comp', directives: [], template: '<div #loc></div>'})
class MyComp3 {
ctxBoolProp: boolean;
@ViewChild('loc', {read: ViewContainerRef}) viewContainerRef: ViewContainerRef;
constructor() { this.ctxBoolProp = false; }
}

View File

@ -527,12 +527,6 @@ export declare abstract class DoCheck {
abstract ngDoCheck(): void;
}
/** @deprecated */
export declare abstract class DynamicComponentLoader {
abstract loadAsRoot(type: Type<any>, overrideSelectorOrNode: string | any, injector: Injector, onDispose?: () => void, projectableNodes?: any[][]): Promise<ComponentRef<any>>;
abstract loadNextToLocation(type: Type<any>, location: ViewContainerRef, providers?: ResolvedReflectiveProvider[], projectableNodes?: any[][]): Promise<ComponentRef<any>>;
}
/** @stable */
export declare class ElementRef {
/** @stable */ nativeElement: any;