refactor(core): type ComponentRef
, ComponentFactory
and ComponentFixture
by the component type
BREAKING CHANGE: - `ComponetRef`, `ComponentFactory`, `ComponentFixture` now all require a type parameter with the component type. Closes #8361
This commit is contained in:
@ -15,7 +15,7 @@ import {isPresent, isBlank} from 'angular2/src/facade/lang';
|
||||
|
||||
@Directive({selector: 'router-outlet'})
|
||||
export class RouterOutlet {
|
||||
private _loaded: ComponentRef;
|
||||
private _loaded: ComponentRef<any>;
|
||||
public outletMap: RouterOutletMap;
|
||||
|
||||
constructor(parentOutletMap: RouterOutletMap, private _location: ViewContainerRef,
|
||||
@ -32,8 +32,8 @@ export class RouterOutlet {
|
||||
|
||||
get isLoaded(): boolean { return isPresent(this._loaded); }
|
||||
|
||||
load(factory: ComponentFactory, providers: ResolvedReflectiveProvider[],
|
||||
outletMap: RouterOutletMap): ComponentRef {
|
||||
load(factory: ComponentFactory<any>, providers: ResolvedReflectiveProvider[],
|
||||
outletMap: RouterOutletMap): ComponentRef<any> {
|
||||
this.outletMap = outletMap;
|
||||
let inj = ReflectiveInjector.fromResolvedProviders(providers, this._location.parentInjector);
|
||||
this._loaded = this._location.createComponent(factory, this._location.length, inj, []);
|
||||
|
@ -86,10 +86,10 @@ export class RouteSegment {
|
||||
_type: Type;
|
||||
|
||||
/** @internal */
|
||||
_componentFactory: ComponentFactory;
|
||||
_componentFactory: ComponentFactory<any>;
|
||||
|
||||
constructor(public urlSegments: UrlSegment[], public parameters: {[key: string]: string},
|
||||
public outlet: string, type: Type, componentFactory: ComponentFactory) {
|
||||
public outlet: string, type: Type, componentFactory: ComponentFactory<any>) {
|
||||
this._type = type;
|
||||
this._componentFactory = componentFactory;
|
||||
}
|
||||
@ -123,6 +123,6 @@ export function equalSegments(a: RouteSegment, b: RouteSegment): boolean {
|
||||
return StringMapWrapper.equals(a.parameters, b.parameters);
|
||||
}
|
||||
|
||||
export function routeSegmentComponentFactory(a: RouteSegment): ComponentFactory {
|
||||
export function routeSegmentComponentFactory(a: RouteSegment): ComponentFactory<any> {
|
||||
return a._componentFactory;
|
||||
}
|
@ -61,17 +61,18 @@ export class OfflineCompiler {
|
||||
var hostMeta = createHostComponentMeta(compMeta.type, compMeta.selector);
|
||||
var hostViewFactoryVar = this._compileComponent(hostMeta, [compMeta], [], statements);
|
||||
var compFactoryVar = `${compMeta.type.name}NgFactory`;
|
||||
statements.push(o.variable(compFactoryVar)
|
||||
.set(o.importExpr(_COMPONENT_FACTORY_IDENTIFIER)
|
||||
.instantiate(
|
||||
[
|
||||
o.literal(compMeta.selector),
|
||||
o.variable(hostViewFactoryVar),
|
||||
o.importExpr(compMeta.type)
|
||||
],
|
||||
o.importType(_COMPONENT_FACTORY_IDENTIFIER, null,
|
||||
[o.TypeModifier.Const])))
|
||||
.toDeclStmt(null, [o.StmtModifier.Final]));
|
||||
statements.push(
|
||||
o.variable(compFactoryVar)
|
||||
.set(o.importExpr(_COMPONENT_FACTORY_IDENTIFIER, [o.importType(compMeta.type)])
|
||||
.instantiate(
|
||||
[
|
||||
o.literal(compMeta.selector),
|
||||
o.variable(hostViewFactoryVar),
|
||||
o.importExpr(compMeta.type)
|
||||
],
|
||||
o.importType(_COMPONENT_FACTORY_IDENTIFIER,
|
||||
[o.importType(compMeta.type)], [o.TypeModifier.Const])))
|
||||
.toDeclStmt(null, [o.StmtModifier.Final]));
|
||||
exportedVars.push(compFactoryVar);
|
||||
});
|
||||
return this._codegenSourceModule(moduleUrl, statements, exportedVars);
|
||||
|
@ -77,7 +77,7 @@ export class RuntimeCompiler implements ComponentResolver {
|
||||
private _viewCompiler: ViewCompiler, private _xhr: XHR,
|
||||
private _genConfig: CompilerConfig) {}
|
||||
|
||||
resolveComponent(componentType: Type): Promise<ComponentFactory> {
|
||||
resolveComponent(componentType: Type): Promise<ComponentFactory<any>> {
|
||||
var compMeta: CompileDirectiveMetadata =
|
||||
this._metadataResolver.getDirectiveMetadata(componentType);
|
||||
var hostCacheKey = this._hostCacheKeys.get(componentType);
|
||||
|
@ -94,8 +94,8 @@ export function getPlatform(): PlatformRef {
|
||||
* Shortcut for ApplicationRef.bootstrap.
|
||||
* Requires a platform the be created first.
|
||||
*/
|
||||
export function coreBootstrap(injector: Injector,
|
||||
componentFactory: ComponentFactory): ComponentRef {
|
||||
export function coreBootstrap<C>(injector: Injector,
|
||||
componentFactory: ComponentFactory<C>): ComponentRef<C> {
|
||||
var appRef: ApplicationRef = injector.get(ApplicationRef);
|
||||
return appRef.bootstrap(componentFactory);
|
||||
}
|
||||
@ -106,7 +106,7 @@ export function coreBootstrap(injector: Injector,
|
||||
* Requires a platform the be created first.
|
||||
*/
|
||||
export function coreLoadAndBootstrap(injector: Injector,
|
||||
componentType: Type): Promise<ComponentRef> {
|
||||
componentType: Type): Promise<ComponentRef<any>> {
|
||||
var appRef: ApplicationRef = injector.get(ApplicationRef);
|
||||
return appRef.run(() => {
|
||||
var componentResolver: ComponentResolver = injector.get(ComponentResolver);
|
||||
@ -190,7 +190,7 @@ export abstract class ApplicationRef {
|
||||
* Register a listener to be called each time `bootstrap()` is called to bootstrap
|
||||
* a new root component.
|
||||
*/
|
||||
abstract registerBootstrapListener(listener: (ref: ComponentRef) => void): void;
|
||||
abstract registerBootstrapListener(listener: (ref: ComponentRef<any>) => void): void;
|
||||
|
||||
/**
|
||||
* Register a listener to be called when the application is disposed.
|
||||
@ -221,7 +221,7 @@ export abstract class ApplicationRef {
|
||||
* ### Example
|
||||
* {@example core/ts/platform/platform.ts region='longform'}
|
||||
*/
|
||||
abstract bootstrap(componentFactory: ComponentFactory): ComponentRef;
|
||||
abstract bootstrap<C>(componentFactory: ComponentFactory<C>): ComponentRef<C>;
|
||||
|
||||
/**
|
||||
* Retrieve the application {@link Injector}.
|
||||
@ -266,7 +266,7 @@ export class ApplicationRef_ extends ApplicationRef {
|
||||
/** @internal */
|
||||
private _disposeListeners: Function[] = [];
|
||||
/** @internal */
|
||||
private _rootComponents: ComponentRef[] = [];
|
||||
private _rootComponents: ComponentRef<any>[] = [];
|
||||
/** @internal */
|
||||
private _rootComponentTypes: Type[] = [];
|
||||
/** @internal */
|
||||
@ -315,7 +315,7 @@ export class ApplicationRef_ extends ApplicationRef {
|
||||
(_) => { this._zone.run(() => { this.tick(); }); });
|
||||
}
|
||||
|
||||
registerBootstrapListener(listener: (ref: ComponentRef) => void): void {
|
||||
registerBootstrapListener(listener: (ref: ComponentRef<any>) => void): void {
|
||||
this._bootstrapListeners.push(listener);
|
||||
}
|
||||
|
||||
@ -357,7 +357,7 @@ export class ApplicationRef_ extends ApplicationRef {
|
||||
return isPromise(result) ? completer.promise : result;
|
||||
}
|
||||
|
||||
bootstrap(componentFactory: ComponentFactory): ComponentRef {
|
||||
bootstrap<C>(componentFactory: ComponentFactory<C>): ComponentRef<C> {
|
||||
if (!this._asyncInitDone) {
|
||||
throw new BaseException(
|
||||
'Cannot bootstrap as there are still asynchronous initializers running. Wait for them using waitForAsyncInitializers().');
|
||||
@ -383,7 +383,7 @@ export class ApplicationRef_ extends ApplicationRef {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_loadComponent(componentRef: ComponentRef): void {
|
||||
_loadComponent(componentRef: ComponentRef<any>): void {
|
||||
this._changeDetectorRefs.push(componentRef.changeDetectorRef);
|
||||
this.tick();
|
||||
this._rootComponents.push(componentRef);
|
||||
@ -391,7 +391,7 @@ export class ApplicationRef_ extends ApplicationRef {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
_unloadComponent(componentRef: ComponentRef): void {
|
||||
_unloadComponent(componentRef: ComponentRef<any>): void {
|
||||
if (!ListWrapper.contains(this._rootComponents, componentRef)) {
|
||||
return;
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import {ChangeDetectorRef} from '../change_detection/change_detection';
|
||||
* Component Instance and allows you to destroy the Component Instance via the {@link #destroy}
|
||||
* method.
|
||||
*/
|
||||
export abstract class ComponentRef {
|
||||
export abstract class ComponentRef<C> {
|
||||
/**
|
||||
* Location of the Host Element of this Component Instance.
|
||||
*/
|
||||
@ -28,7 +28,7 @@ export abstract class ComponentRef {
|
||||
/**
|
||||
* The instance of the Component.
|
||||
*/
|
||||
get instance(): any { return unimplemented(); };
|
||||
get instance(): C { return unimplemented(); };
|
||||
|
||||
/**
|
||||
* The {@link ViewRef} of the Host View of this Component instance.
|
||||
@ -56,11 +56,11 @@ export abstract class ComponentRef {
|
||||
abstract onDestroy(callback: Function): void;
|
||||
}
|
||||
|
||||
export class ComponentRef_ extends ComponentRef {
|
||||
export class ComponentRef_<C> extends ComponentRef<C> {
|
||||
constructor(private _hostElement: AppElement, private _componentType: Type) { super(); }
|
||||
get location(): ElementRef { return this._hostElement.elementRef; }
|
||||
get injector(): Injector { return this._hostElement.injector; }
|
||||
get instance(): any { return this._hostElement.component; };
|
||||
get instance(): C { return this._hostElement.component; };
|
||||
get hostView(): ViewRef { return this._hostElement.parentView.ref; };
|
||||
get changeDetectorRef(): ChangeDetectorRef { return this._hostElement.parentView.ref; };
|
||||
get componentType(): Type { return this._componentType; }
|
||||
@ -72,7 +72,7 @@ export class ComponentRef_ extends ComponentRef {
|
||||
const EMPTY_CONTEXT = /*@ts2dart_const*/ new Object();
|
||||
|
||||
/*@ts2dart_const*/
|
||||
export class ComponentFactory {
|
||||
export class ComponentFactory<C> {
|
||||
constructor(public selector: string, private _viewFactory: Function,
|
||||
private _componentType: Type) {}
|
||||
|
||||
@ -82,7 +82,7 @@ export class ComponentFactory {
|
||||
* Creates a new component.
|
||||
*/
|
||||
create(injector: Injector, projectableNodes: any[][] = null,
|
||||
rootSelectorOrNode: string | any = null): ComponentRef {
|
||||
rootSelectorOrNode: string | any = null): ComponentRef<C> {
|
||||
var vu: ViewUtils = injector.get(ViewUtils);
|
||||
if (isBlank(projectableNodes)) {
|
||||
projectableNodes = [];
|
||||
@ -90,6 +90,6 @@ export class ComponentFactory {
|
||||
// Note: Host views don't need a declarationAppElement!
|
||||
var hostView = this._viewFactory(vu, injector, null);
|
||||
var hostElement = hostView.create(EMPTY_CONTEXT, projectableNodes, rootSelectorOrNode);
|
||||
return new ComponentRef_(hostElement, this._componentType);
|
||||
return new ComponentRef_<C>(hostElement, this._componentType);
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import {ComponentFactory} from './component_factory';
|
||||
* can later be used to create and render a Component instance.
|
||||
*/
|
||||
export abstract class ComponentResolver {
|
||||
abstract resolveComponent(componentType: Type): Promise<ComponentFactory>;
|
||||
abstract resolveComponent(componentType: Type): Promise<ComponentFactory<any>>;
|
||||
abstract clearCache();
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ function _isComponentFactory(type: any): boolean {
|
||||
|
||||
@Injectable()
|
||||
export class ReflectorComponentResolver extends ComponentResolver {
|
||||
resolveComponent(componentType: Type): Promise<ComponentFactory> {
|
||||
resolveComponent(componentType: Type): Promise<ComponentFactory<any>> {
|
||||
var metadatas = reflector.annotations(componentType);
|
||||
var componentFactory = metadatas.find(_isComponentFactory);
|
||||
|
||||
|
@ -65,7 +65,8 @@ export abstract class DynamicComponentLoader {
|
||||
* ```
|
||||
*/
|
||||
abstract loadAsRoot(type: Type, overrideSelectorOrNode: string | any, injector: Injector,
|
||||
onDispose?: () => void, projectableNodes?: any[][]): Promise<ComponentRef>;
|
||||
onDispose?: () => void,
|
||||
projectableNodes?: any[][]): Promise<ComponentRef<any>>;
|
||||
|
||||
|
||||
/**
|
||||
@ -110,7 +111,7 @@ export abstract class DynamicComponentLoader {
|
||||
*/
|
||||
abstract loadNextToLocation(type: Type, location: ViewContainerRef,
|
||||
providers?: ResolvedReflectiveProvider[],
|
||||
projectableNodes?: any[][]): Promise<ComponentRef>;
|
||||
projectableNodes?: any[][]): Promise<ComponentRef<any>>;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
@ -118,7 +119,7 @@ export class DynamicComponentLoader_ extends DynamicComponentLoader {
|
||||
constructor(private _compiler: ComponentResolver) { super(); }
|
||||
|
||||
loadAsRoot(type: Type, overrideSelectorOrNode: string | any, injector: Injector,
|
||||
onDispose?: () => void, projectableNodes?: any[][]): Promise<ComponentRef> {
|
||||
onDispose?: () => void, projectableNodes?: any[][]): Promise<ComponentRef<any>> {
|
||||
return this._compiler.resolveComponent(type).then(componentFactory => {
|
||||
var componentRef = componentFactory.create(
|
||||
injector, projectableNodes,
|
||||
@ -132,7 +133,7 @@ export class DynamicComponentLoader_ extends DynamicComponentLoader {
|
||||
|
||||
loadNextToLocation(type: Type, location: ViewContainerRef,
|
||||
providers: ResolvedReflectiveProvider[] = null,
|
||||
projectableNodes: any[][] = null): Promise<ComponentRef> {
|
||||
projectableNodes: any[][] = null): Promise<ComponentRef<any>> {
|
||||
return this._compiler.resolveComponent(type).then(componentFactory => {
|
||||
var contextInjector = location.parentInjector;
|
||||
var childInjector = isPresent(providers) && providers.length > 0 ?
|
||||
|
@ -62,9 +62,8 @@ export abstract class ViewContainerRef {
|
||||
*
|
||||
* Returns the {@link ViewRef} for the newly created View.
|
||||
*/
|
||||
// TODO(tbosch): Use a generic once ts2dart supports it.
|
||||
abstract createEmbeddedView(templateRef: TemplateRef<any>, context?: any,
|
||||
index?: number): EmbeddedViewRef<any>;
|
||||
abstract createEmbeddedView<C>(templateRef: TemplateRef<C>, context?: C,
|
||||
index?: number): EmbeddedViewRef<C>;
|
||||
|
||||
/**
|
||||
* Instantiates a single {@link Component} and inserts its Host View into this container at the
|
||||
@ -79,8 +78,8 @@ export abstract class ViewContainerRef {
|
||||
*
|
||||
* Returns the {@link ComponentRef} of the Host View created for the newly instantiated Component.
|
||||
*/
|
||||
abstract createComponent(componentFactory: ComponentFactory, index?: number, injector?: Injector,
|
||||
projectableNodes?: any[][]): ComponentRef;
|
||||
abstract createComponent<C>(componentFactory: ComponentFactory<C>, index?: number,
|
||||
injector?: Injector, projectableNodes?: any[][]): ComponentRef<C>;
|
||||
|
||||
/**
|
||||
* Inserts a View identified by a {@link ViewRef} into the container at the specified `index`.
|
||||
@ -129,9 +128,8 @@ export class ViewContainerRef_ implements ViewContainerRef {
|
||||
|
||||
// TODO(rado): profile and decide whether bounds checks should be added
|
||||
// to the methods below.
|
||||
// TODO(tbosch): use a generic C once ts2dart supports it.
|
||||
createEmbeddedView(templateRef: TemplateRef<any>, context: any = null,
|
||||
index: number = -1): EmbeddedViewRef<any> {
|
||||
createEmbeddedView<C>(templateRef: TemplateRef<C>, context: C = null,
|
||||
index: number = -1): EmbeddedViewRef<C> {
|
||||
var viewRef: EmbeddedViewRef<any> = templateRef.createEmbeddedView(context);
|
||||
this.insert(viewRef, index);
|
||||
return viewRef;
|
||||
@ -141,8 +139,8 @@ export class ViewContainerRef_ implements ViewContainerRef {
|
||||
_createComponentInContainerScope: WtfScopeFn =
|
||||
wtfCreateScope('ViewContainerRef#createComponent()');
|
||||
|
||||
createComponent(componentFactory: ComponentFactory, index: number = -1, injector: Injector = null,
|
||||
projectableNodes: any[][] = null): ComponentRef {
|
||||
createComponent<C>(componentFactory: ComponentFactory<C>, index: number = -1,
|
||||
injector: Injector = null, projectableNodes: any[][] = null): ComponentRef<C> {
|
||||
var s = this._createComponentInContainerScope();
|
||||
var contextInjector = isPresent(injector) ? injector : this._element.parentInjector;
|
||||
var componentRef = componentFactory.create(contextInjector, projectableNodes);
|
||||
|
@ -9,11 +9,11 @@ import {NgZone} from 'angular2/src/core/zone/ng_zone';
|
||||
*/
|
||||
@Injectable()
|
||||
export class MockApplicationRef extends ApplicationRef {
|
||||
registerBootstrapListener(listener: (ref: ComponentRef) => void): void {}
|
||||
registerBootstrapListener(listener: (ref: ComponentRef<any>) => void): void {}
|
||||
|
||||
registerDisposeListener(dispose: () => void): void {}
|
||||
|
||||
bootstrap(componentFactory: ComponentFactory): ComponentRef { return null; }
|
||||
bootstrap<C>(componentFactory: ComponentFactory<C>): ComponentRef<C> { return null; }
|
||||
|
||||
get injector(): Injector { return null; };
|
||||
|
||||
|
@ -15,7 +15,7 @@ export class ChangeDetectionPerfRecord {
|
||||
export class AngularTools {
|
||||
profiler: AngularProfiler;
|
||||
|
||||
constructor(ref: ComponentRef) { this.profiler = new AngularProfiler(ref); }
|
||||
constructor(ref: ComponentRef<any>) { this.profiler = new AngularProfiler(ref); }
|
||||
}
|
||||
|
||||
/**
|
||||
@ -25,7 +25,7 @@ export class AngularTools {
|
||||
export class AngularProfiler {
|
||||
appRef: ApplicationRef;
|
||||
|
||||
constructor(ref: ComponentRef) { this.appRef = ref.injector.get(ApplicationRef); }
|
||||
constructor(ref: ComponentRef<any>) { this.appRef = ref.injector.get(ApplicationRef); }
|
||||
|
||||
/**
|
||||
* Exercises change detection in a loop and then prints the average amount of
|
||||
|
@ -16,7 +16,7 @@ import 'common_tools.dart' show AngularTools;
|
||||
* 1. Try the change detection profiler `ng.profiler.timeChangeDetection()`
|
||||
* then hit Enter.
|
||||
*/
|
||||
void enableDebugTools(ComponentRef ref) {
|
||||
void enableDebugTools(ComponentRef<dynamic> ref) {
|
||||
final tools = new AngularTools(ref);
|
||||
context['ng'] = new JsObject.jsify({
|
||||
'profiler': {
|
||||
|
@ -15,7 +15,7 @@ var context = <any>global;
|
||||
* 1. Try the change detection profiler `ng.profiler.timeChangeDetection()`
|
||||
* then hit Enter.
|
||||
*/
|
||||
export function enableDebugTools(ref: ComponentRef): void {
|
||||
export function enableDebugTools(ref: ComponentRef<any>): void {
|
||||
context.ng = new AngularTools(ref);
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,7 @@ let _resolveToTrue = PromiseWrapper.resolve(true);
|
||||
@Directive({selector: 'router-outlet'})
|
||||
export class RouterOutlet implements OnDestroy {
|
||||
name: string = null;
|
||||
private _componentRef: Promise<ComponentRef> = null;
|
||||
private _componentRef: Promise<ComponentRef<any>> = null;
|
||||
private _currentInstruction: ComponentInstruction = null;
|
||||
|
||||
@Output('activate') public activateEvents = new EventEmitter<any>();
|
||||
@ -70,7 +70,7 @@ export class RouterOutlet implements OnDestroy {
|
||||
this.activateEvents.emit(componentRef.instance);
|
||||
if (hasLifecycleHook(hookMod.routerOnActivate, componentType)) {
|
||||
return this._componentRef.then(
|
||||
(ref: ComponentRef) =>
|
||||
(ref: ComponentRef<any>) =>
|
||||
(<OnActivate>ref.instance).routerOnActivate(nextInstruction, previousInstruction));
|
||||
} else {
|
||||
return componentRef;
|
||||
@ -96,7 +96,7 @@ export class RouterOutlet implements OnDestroy {
|
||||
return PromiseWrapper.resolve(
|
||||
hasLifecycleHook(hookMod.routerOnReuse, this._currentInstruction.componentType) ?
|
||||
this._componentRef.then(
|
||||
(ref: ComponentRef) =>
|
||||
(ref: ComponentRef<any>) =>
|
||||
(<OnReuse>ref.instance).routerOnReuse(nextInstruction, previousInstruction)) :
|
||||
true);
|
||||
}
|
||||
@ -111,13 +111,13 @@ export class RouterOutlet implements OnDestroy {
|
||||
if (isPresent(this._componentRef) && isPresent(this._currentInstruction) &&
|
||||
hasLifecycleHook(hookMod.routerOnDeactivate, this._currentInstruction.componentType)) {
|
||||
next = this._componentRef.then(
|
||||
(ref: ComponentRef) =>
|
||||
(ref: ComponentRef<any>) =>
|
||||
(<OnDeactivate>ref.instance)
|
||||
.routerOnDeactivate(nextInstruction, this._currentInstruction));
|
||||
}
|
||||
return next.then((_) => {
|
||||
if (isPresent(this._componentRef)) {
|
||||
var onDispose = this._componentRef.then((ref: ComponentRef) => ref.destroy());
|
||||
var onDispose = this._componentRef.then((ref: ComponentRef<any>) => ref.destroy());
|
||||
this._componentRef = null;
|
||||
return onDispose;
|
||||
}
|
||||
@ -138,7 +138,7 @@ export class RouterOutlet implements OnDestroy {
|
||||
}
|
||||
if (hasLifecycleHook(hookMod.routerCanDeactivate, this._currentInstruction.componentType)) {
|
||||
return this._componentRef.then(
|
||||
(ref: ComponentRef) =>
|
||||
(ref: ComponentRef<any>) =>
|
||||
(<CanDeactivate>ref.instance)
|
||||
.routerCanDeactivate(nextInstruction, this._currentInstruction));
|
||||
} else {
|
||||
@ -164,7 +164,7 @@ export class RouterOutlet implements OnDestroy {
|
||||
result = false;
|
||||
} else if (hasLifecycleHook(hookMod.routerCanReuse, this._currentInstruction.componentType)) {
|
||||
result = this._componentRef.then(
|
||||
(ref: ComponentRef) =>
|
||||
(ref: ComponentRef<any>) =>
|
||||
(<CanReuse>ref.instance).routerCanReuse(nextInstruction, this._currentInstruction));
|
||||
} else {
|
||||
result = nextInstruction == this._currentInstruction ||
|
||||
|
@ -1,7 +1,8 @@
|
||||
import {
|
||||
OpaqueToken,
|
||||
ComponentRef,
|
||||
DynamicComponentLoader,
|
||||
ComponentFactory,
|
||||
ComponentResolver,
|
||||
Injector,
|
||||
Injectable,
|
||||
ViewMetadata,
|
||||
@ -34,7 +35,7 @@ export var ComponentFixtureNoNgZone = new OpaqueToken("ComponentFixtureNoNgZone"
|
||||
/**
|
||||
* Fixture for debugging and testing a component.
|
||||
*/
|
||||
export class ComponentFixture {
|
||||
export class ComponentFixture<T> {
|
||||
/**
|
||||
* The DebugElement associated with the root element of this component.
|
||||
*/
|
||||
@ -58,7 +59,7 @@ export class ComponentFixture {
|
||||
/**
|
||||
* The ComponentRef for the component
|
||||
*/
|
||||
componentRef: ComponentRef;
|
||||
componentRef: ComponentRef<T>;
|
||||
|
||||
/**
|
||||
* The ChangeDetectorRef for the component
|
||||
@ -79,7 +80,7 @@ export class ComponentFixture {
|
||||
private _onMicrotaskEmptySubscription = null;
|
||||
private _onErrorSubscription = null;
|
||||
|
||||
constructor(componentRef: ComponentRef, ngZone: NgZone, autoDetect: boolean) {
|
||||
constructor(componentRef: ComponentRef<T>, ngZone: NgZone, autoDetect: boolean) {
|
||||
this.changeDetectorRef = componentRef.changeDetectorRef;
|
||||
this.elementRef = componentRef.location;
|
||||
this.debugElement = <DebugElement>getDebugNode(this.elementRef.nativeElement);
|
||||
@ -334,15 +335,30 @@ export class TestComponentBuilder {
|
||||
return this.overrideViewProviders(type, providers);
|
||||
}
|
||||
|
||||
private _create<C>(ngZone: NgZone, componentFactory: ComponentFactory<C>): ComponentFixture<C> {
|
||||
let rootElId = `root${_nextRootElementId++}`;
|
||||
let rootEl = el(`<div id="${rootElId}"></div>`);
|
||||
let doc = this._injector.get(DOCUMENT);
|
||||
|
||||
// TODO(juliemr): can/should this be optional?
|
||||
let oldRoots = DOM.querySelectorAll(doc, '[id^=root]');
|
||||
for (let i = 0; i < oldRoots.length; i++) {
|
||||
DOM.remove(oldRoots[i]);
|
||||
}
|
||||
DOM.appendChild(doc.body, rootEl);
|
||||
var componentRef = componentFactory.create(this._injector, [], `#${rootElId}`);
|
||||
let autoDetect: boolean = this._injector.get(ComponentFixtureAutoDetect, false);
|
||||
return new ComponentFixture<any /*C*/>(componentRef, ngZone, autoDetect);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds and returns a ComponentFixture.
|
||||
*
|
||||
* @return {Promise<ComponentFixture>}
|
||||
*/
|
||||
createAsync(rootComponentType: Type): Promise<ComponentFixture> {
|
||||
createAsync(rootComponentType: Type): Promise<ComponentFixture<any>> {
|
||||
let noNgZone = IS_DART || this._injector.get(ComponentFixtureNoNgZone, false);
|
||||
let ngZone: NgZone = noNgZone ? null : this._injector.get(NgZone, null);
|
||||
let autoDetect: boolean = this._injector.get(ComponentFixtureAutoDetect, false);
|
||||
|
||||
let initComponent = () => {
|
||||
let mockDirectiveResolver = this._injector.get(DirectiveResolver);
|
||||
@ -359,28 +375,15 @@ export class TestComponentBuilder {
|
||||
this._viewBindingsOverrides.forEach(
|
||||
(bindings, type) => mockDirectiveResolver.setViewBindingsOverride(type, bindings));
|
||||
|
||||
let rootElId = `root${_nextRootElementId++}`;
|
||||
let rootEl = el(`<div id="${rootElId}"></div>`);
|
||||
let doc = this._injector.get(DOCUMENT);
|
||||
|
||||
// TODO(juliemr): can/should this be optional?
|
||||
let oldRoots = DOM.querySelectorAll(doc, '[id^=root]');
|
||||
for (let i = 0; i < oldRoots.length; i++) {
|
||||
DOM.remove(oldRoots[i]);
|
||||
}
|
||||
DOM.appendChild(doc.body, rootEl);
|
||||
|
||||
let promise: Promise<ComponentRef> =
|
||||
this._injector.get(DynamicComponentLoader)
|
||||
.loadAsRoot(rootComponentType, `#${rootElId}`, this._injector);
|
||||
return promise.then(
|
||||
(componentRef) => { return new ComponentFixture(componentRef, ngZone, autoDetect); });
|
||||
let promise: Promise<ComponentFactory<any>> =
|
||||
this._injector.get(ComponentResolver).resolveComponent(rootComponentType);
|
||||
return promise.then(componentFactory => this._create(ngZone, componentFactory));
|
||||
};
|
||||
|
||||
return ngZone == null ? initComponent() : ngZone.run(initComponent);
|
||||
}
|
||||
|
||||
createFakeAsync(rootComponentType: Type): ComponentFixture {
|
||||
createFakeAsync(rootComponentType: Type): ComponentFixture<any> {
|
||||
let result;
|
||||
let error;
|
||||
PromiseWrapper.then(this.createAsync(rootComponentType), (_result) => { result = _result; },
|
||||
@ -391,4 +394,12 @@ export class TestComponentBuilder {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
createSync<C>(componentFactory: ComponentFactory<C>): ComponentFixture<C> {
|
||||
let noNgZone = IS_DART || this._injector.get(ComponentFixtureNoNgZone, false);
|
||||
let ngZone: NgZone = noNgZone ? null : this._injector.get(NgZone, null);
|
||||
|
||||
let initComponent = () => this._create(ngZone, componentFactory);
|
||||
return ngZone == null ? initComponent() : ngZone.run(initComponent);
|
||||
}
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ export class DowngradeNg2ComponentAdapter {
|
||||
component: any = null;
|
||||
inputChangeCount: number = 0;
|
||||
inputChanges: {[key: string]: SimpleChange} = null;
|
||||
componentRef: ComponentRef = null;
|
||||
componentRef: ComponentRef<any> = null;
|
||||
changeDetector: ChangeDetectorRef = null;
|
||||
componentScope: angular.IScope;
|
||||
childNodes: Node[];
|
||||
@ -30,7 +30,8 @@ export class DowngradeNg2ComponentAdapter {
|
||||
constructor(private id: string, private info: ComponentInfo,
|
||||
private element: angular.IAugmentedJQuery, private attrs: angular.IAttributes,
|
||||
private scope: angular.IScope, private parentInjector: Injector,
|
||||
private parse: angular.IParseService, private componentFactory: ComponentFactory) {
|
||||
private parse: angular.IParseService,
|
||||
private componentFactory: ComponentFactory<any>) {
|
||||
(<any>this.element[0]).id = id;
|
||||
this.componentScope = scope.$new();
|
||||
this.childNodes = <Node[]><any>element.contents();
|
||||
|
@ -524,12 +524,12 @@ export class UpgradeAdapter {
|
||||
private compileNg2Components(compiler: ComponentResolver,
|
||||
componentFactoryRefMap: ComponentFactoryRefMap):
|
||||
Promise<ComponentFactoryRefMap> {
|
||||
var promises: Array<Promise<ComponentFactory>> = [];
|
||||
var promises: Array<Promise<ComponentFactory<any>>> = [];
|
||||
var types = this.upgradedComponents;
|
||||
for (var i = 0; i < types.length; i++) {
|
||||
promises.push(compiler.resolveComponent(types[i]));
|
||||
}
|
||||
return Promise.all(promises).then((componentFactories: Array<ComponentFactory>) => {
|
||||
return Promise.all(promises).then((componentFactories: Array<ComponentFactory<any>>) => {
|
||||
var types = this.upgradedComponents;
|
||||
for (var i = 0; i < componentFactories.length; i++) {
|
||||
componentFactoryRefMap[getComponentInfo(types[i]).selector] = componentFactories[i];
|
||||
@ -540,14 +540,14 @@ export class UpgradeAdapter {
|
||||
}
|
||||
|
||||
interface ComponentFactoryRefMap {
|
||||
[selector: string]: ComponentFactory;
|
||||
[selector: string]: ComponentFactory<any>;
|
||||
}
|
||||
|
||||
function ng1ComponentDirective(info: ComponentInfo, idPrefix: string): Function {
|
||||
(<any>directiveFactory).$inject = [NG2_COMPONENT_FACTORY_REF_MAP, NG1_PARSE];
|
||||
function directiveFactory(componentFactoryRefMap: ComponentFactoryRefMap,
|
||||
parse: angular.IParseService): angular.IDirective {
|
||||
var componentFactory: ComponentFactory = componentFactoryRefMap[info.selector];
|
||||
var componentFactory: ComponentFactory<any> = componentFactoryRefMap[info.selector];
|
||||
if (!componentFactory) throw new Error('Expecting ComponentFactory for: ' + info.selector);
|
||||
var idCount = 0;
|
||||
return {
|
||||
|
Reference in New Issue
Block a user