refactor(core): introduce ComponentFactory.

Each compile template now exposes a `<CompName>NgFactory` variable
with an instance of a `ComponentFactory`.
Calling `ComponentFactory.create` returns a `ComponentRef` that can
be used directly.

BREAKING CHANGE:
- `Compiler` is renamed to `ComponentResolver`,
  `Compiler.compileInHost` has been renamed to `ComponentResolver.resolveComponent`.
- `ComponentRef.dispose` is renamed to `ComponentRef.destroy`
- `ViewContainerRef.createHostView` is renamed to `ViewContainerRef.createComponent`
- `ComponentFixture_` has been removed, the class `ComponentFixture`
  can now be created directly as it is no more using private APIs.
This commit is contained in:
Tobias Bosch
2016-04-13 17:05:17 -07:00
parent 41404057cf
commit 0c600cf6e3
66 changed files with 611 additions and 849 deletions

View File

@ -4,9 +4,9 @@ import {TypeScriptEmitter} from 'angular2/src/compiler/output/ts_emitter';
import {DartEmitter} from 'angular2/src/compiler/output/dart_emitter';
import * as o from 'angular2/src/compiler/output/output_ast';
import {compileComp, compAMetadata} from './offline_compiler_util';
import {HostViewFactory} from 'angular2/src/core/linker/view';
import {ComponentFactory} from 'angular2/src/core/linker/component_factory';
export const hostViewFactory_CompA: HostViewFactory = null;
export const CompANgFactory: ComponentFactory = null;
// Generator
export function main(args: string[]) {

View File

@ -2,9 +2,9 @@
import {print} from 'angular2/src/facade/lang';
import {JavaScriptEmitter} from 'angular2/src/compiler/output/js_emitter';
import {compileComp, compAMetadata} from './offline_compiler_util';
import {HostViewFactory} from 'angular2/src/core/linker/view';
import {ComponentFactory} from 'angular2/src/core/linker/component_factory';
export const hostViewFactory_CompA: HostViewFactory = null;
export const CompANgFactory: ComponentFactory = null;
// Generator
export function main(args: string[]) {

View File

@ -18,65 +18,48 @@ import {IS_DART} from 'angular2/src/facade/lang';
import {Injector} from 'angular2/core';
import {DebugNode, DebugElement, getDebugNode} from 'angular2/src/core/debug/debug_node';
import {HostViewFactoryRef_} from 'angular2/src/core/linker/view_ref';
import {HostViewFactory} from 'angular2/src/core/linker/view';
import {ComponentFactory} from 'angular2/src/core/linker/component_factory';
import * as typed from './offline_compiler_codegen_typed';
import * as untyped from './offline_compiler_codegen_untyped';
import {AppViewManager} from 'angular2/src/core/linker/view_manager';
import {DOCUMENT} from 'angular2/src/platform/dom/dom_tokens';
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
import {SharedStylesHost} from "angular2/src/platform/dom/shared_styles_host";
import {CompA} from './offline_compiler_util';
var _nextRootElementId = 0;
export function main() {
var outputDefs = [];
var typedHostViewFactory = typed.hostViewFactory_CompA;
var untypedHostViewFactory = untyped.hostViewFactory_CompA;
var typedComponentFactory = typed.CompANgFactory;
var untypedComponentFactory = untyped.CompANgFactory;
if (IS_DART || !DOM.supportsDOMEvents()) {
// Our generator only works on node.js and Dart...
outputDefs.push({'compAHostViewFactory': typedHostViewFactory, 'name': 'typed'});
outputDefs.push({'compAHostComponentFactory': typedComponentFactory, 'name': 'typed'});
}
if (!IS_DART) {
// Our generator only works on node.js and Dart...
if (!DOM.supportsDOMEvents()) {
outputDefs.push({'compAHostViewFactory': untypedHostViewFactory, 'name': 'untyped'});
outputDefs.push({'compAHostComponentFactory': untypedComponentFactory, 'name': 'untyped'});
}
}
describe('OfflineCompiler', () => {
var viewManager: AppViewManager;
var injector: Injector;
var sharedStylesHost: SharedStylesHost;
var rootEl;
beforeEach(inject([AppViewManager, Injector, SharedStylesHost],
(_viewManager, _injector, _sharedStylesHost) => {
viewManager = _viewManager;
injector = _injector;
sharedStylesHost = _sharedStylesHost;
}));
beforeEach(inject([Injector, SharedStylesHost], (_injector, _sharedStylesHost) => {
injector = _injector;
sharedStylesHost = _sharedStylesHost;
}));
function createHostComp(hvf: HostViewFactory): DebugElement {
var doc = injector.get(DOCUMENT);
var oldRoots = DOM.querySelectorAll(doc, hvf.selector);
for (var i = 0; i < oldRoots.length; i++) {
DOM.remove(oldRoots[i]);
}
rootEl = el(`<${hvf.selector}></${hvf.selector}>`);
DOM.appendChild(doc.body, rootEl);
viewManager.createRootHostView(new HostViewFactoryRef_(hvf), hvf.selector, injector, []);
return <DebugElement>getDebugNode(rootEl);
function createHostComp(cf: ComponentFactory): DebugElement {
var compRef = cf.create(injector);
return <DebugElement>getDebugNode(compRef.location.nativeElement);
}
outputDefs.forEach((outputDef) => {
describe(`${outputDef['name']}`, () => {
it('should compile components', () => {
var hostEl = createHostComp(outputDef['compAHostViewFactory']);
var hostEl = createHostComp(outputDef['compAHostComponentFactory']);
expect(hostEl.componentInstance).toBeAnInstanceOf(CompA);
var styles = sharedStylesHost.getAllStyles();
expect(styles[0]).toContain('.redStyle[_ngcontent');