feat(bootstraping): application bootstrapping implementation.
Entry-point to bootstrapping is a rootComponent. The bootstrapping method, uses the component's selector to find the insertion element in the DOM, and attaches the component in its ShadowRoot.
This commit is contained in:
85
modules/core/src/application.js
Normal file
85
modules/core/src/application.js
Normal file
@ -0,0 +1,85 @@
|
||||
import {Injector, bind} from 'di/di';
|
||||
import {Type, FIELD, isBlank, isPresent, BaseException} from 'facade/lang';
|
||||
import {DOM, Element} from 'facade/dom';
|
||||
import {Compiler} from './compiler/compiler';
|
||||
import {ProtoView} from './compiler/view';
|
||||
import {ClosureMap} from 'change_detection/parser/closure_map';
|
||||
import {Parser} from 'change_detection/parser/parser';
|
||||
import {Lexer} from 'change_detection/parser/lexer';
|
||||
import {ChangeDetector} from 'change_detection/change_detector';
|
||||
import {WatchGroup} from 'change_detection/watch_group';
|
||||
import {TemplateLoader} from './compiler/template_loader';
|
||||
import {Reflector} from './compiler/reflector';
|
||||
import {AnnotatedType} from './compiler/annotated_type';
|
||||
import {ListWrapper} from 'facade/collection';
|
||||
|
||||
var _rootInjector: Injector;
|
||||
|
||||
// Contains everything that is safe to share between applications.
|
||||
var _rootBindings = [Compiler, TemplateLoader, Reflector, Parser, Lexer, ClosureMap];
|
||||
|
||||
export var appViewToken = new Object();
|
||||
export var appWatchGroupToken = new Object();
|
||||
export var appElementToken = new Object();
|
||||
export var appComponentAnnotatedTypeToken = new Object();
|
||||
export var appDocumentToken = new Object();
|
||||
|
||||
// Exported only for tests that need to overwrite default document binding.
|
||||
export function documentDependentBindings(appComponentType) {
|
||||
return [
|
||||
bind(appComponentAnnotatedTypeToken).toFactory((reflector) => {
|
||||
// TODO(rado): inspect annotation here and warn if there are bindings,
|
||||
// lightDomServices, and other component annotations that are skipped
|
||||
// for bootstrapping components.
|
||||
return reflector.annotatedType(appComponentType);
|
||||
}, [Reflector]),
|
||||
|
||||
bind(appElementToken).toFactory((appComponentAnnotatedType, appDocument) => {
|
||||
var selector = appComponentAnnotatedType.annotation.selector;
|
||||
var element = DOM.querySelector(appDocument, selector);
|
||||
if (isBlank(element)) {
|
||||
throw new BaseException(`The app selector "${selector}" did not match any elements`);
|
||||
}
|
||||
return element;
|
||||
}, [appComponentAnnotatedTypeToken, appDocumentToken]),
|
||||
|
||||
bind(appViewToken).toAsyncFactory((compiler, injector, appElement,
|
||||
appComponentAnnotatedType) => {
|
||||
return compiler.compile(appComponentAnnotatedType.type, null).then(
|
||||
(protoView) => {
|
||||
var appProtoView = ProtoView.createRootProtoView(protoView,
|
||||
appElement, appComponentAnnotatedType);
|
||||
// The light Dom of the app element is not considered part of
|
||||
// the angular application. Thus the context and lightDomInjector are
|
||||
// empty.
|
||||
return appProtoView.instantiate(new Object(), injector, null, true);
|
||||
});
|
||||
}, [Compiler, Injector, appElementToken, appComponentAnnotatedTypeToken]),
|
||||
|
||||
bind(appWatchGroupToken).toFactory((rootView) => rootView.watchGroup,
|
||||
[appViewToken]),
|
||||
bind(ChangeDetector).toFactory((appWatchGroup) =>
|
||||
new ChangeDetector(appWatchGroup), [appWatchGroupToken])
|
||||
];
|
||||
}
|
||||
|
||||
function _injectorBindings(appComponentType) {
|
||||
return ListWrapper.concat([bind(appDocumentToken).toValue(DOM.defaultDoc())],
|
||||
documentDependentBindings(appComponentType));
|
||||
}
|
||||
|
||||
// Multiple calls to this method are allowed. Each application would only share
|
||||
// _rootInjector, which is not user-configurable by design, thus safe to share.
|
||||
export function bootstrap(appComponentType: Type, bindings=null) {
|
||||
// TODO(rado): prepopulate template cache, so applications with only
|
||||
// index.html and main.js are possible.
|
||||
if (isBlank(_rootInjector)) _rootInjector = new Injector(_rootBindings);
|
||||
var appInjector = _rootInjector.createChild(_injectorBindings(
|
||||
appComponentType));
|
||||
if (isPresent(bindings)) appInjector = appInjector.createChild(bindings);
|
||||
return appInjector.asyncGet(ChangeDetector).then((cd) => {
|
||||
// TODO(rado): replace with zone.
|
||||
cd.detectChanges();
|
||||
return appInjector;
|
||||
});
|
||||
}
|
@ -13,6 +13,7 @@ import {CompileElement} from './pipeline/compile_element';
|
||||
import {createDefaultSteps} from './pipeline/default_steps';
|
||||
import {TemplateLoader} from './template_loader';
|
||||
import {AnnotatedType} from './annotated_type';
|
||||
import {Component} from '../annotations/component';
|
||||
|
||||
/**
|
||||
* The compiler loads and translates the html templates of components into
|
||||
@ -28,7 +29,8 @@ export class Compiler {
|
||||
}
|
||||
|
||||
createSteps(component:AnnotatedType):List<CompileStep> {
|
||||
var directives = component.annotation.template.directives;
|
||||
var annotation: Component = component.annotation;
|
||||
var directives = annotation.template.directives;
|
||||
var annotatedDirectives = ListWrapper.create();
|
||||
for (var i=0; i<directives.length; i++) {
|
||||
ListWrapper.push(annotatedDirectives, this._reflector.annotatedType(directives[i]));
|
||||
@ -51,7 +53,8 @@ export class Compiler {
|
||||
// - templateRoot string
|
||||
// - precompiled template
|
||||
// - ProtoView
|
||||
templateRoot = DOM.createTemplate(component.annotation.template.inline);
|
||||
var annotation: Component = component.annotation;
|
||||
templateRoot = DOM.createTemplate(annotation.template.inline);
|
||||
}
|
||||
var pipeline = new CompilePipeline(this.createSteps(component));
|
||||
var compileElements = pipeline.process(templateRoot);
|
||||
|
@ -83,8 +83,9 @@ export class ProtoView {
|
||||
this.elementsWithBindingCount = 0;
|
||||
}
|
||||
|
||||
instantiate(context, lightDomAppInjector:Injector, hostElementInjector: ElementInjector):View {
|
||||
var clone = DOM.clone(this.element);
|
||||
instantiate(context, lightDomAppInjector:Injector,
|
||||
hostElementInjector: ElementInjector, inPlace:boolean = false):View {
|
||||
var clone = inPlace ? this.element : DOM.clone(this.element);
|
||||
var elements;
|
||||
if (clone instanceof TemplateElement) {
|
||||
elements = ListWrapper.clone(DOM.querySelectorAll(clone.content, `.${NG_BINDING_CLASS}`));
|
||||
@ -260,6 +261,20 @@ export class ProtoView {
|
||||
}
|
||||
return injectors;
|
||||
}
|
||||
|
||||
// Create a rootView as if the compiler encountered <rootcmp></rootcmp>,
|
||||
// and the component template is already compiled into protoView.
|
||||
// Used for bootstrapping.
|
||||
static createRootProtoView(protoView: ProtoView,
|
||||
insertionElement, rootComponentAnnotatedType: AnnotatedType): ProtoView {
|
||||
var rootProtoView = new ProtoView(insertionElement, new ProtoWatchGroup());
|
||||
var binder = rootProtoView.bindElement(
|
||||
new ProtoElementInjector(null, 0, [rootComponentAnnotatedType.type], true));
|
||||
binder.componentDirective = rootComponentAnnotatedType;
|
||||
binder.nestedProtoView = protoView;
|
||||
DOM.addClass(insertionElement, 'ng-binding');
|
||||
return rootProtoView;
|
||||
}
|
||||
}
|
||||
|
||||
export class ElementPropertyMemento {
|
||||
|
@ -5,6 +5,8 @@ export * from './annotations/directive';
|
||||
export * from './annotations/component';
|
||||
export * from './annotations/template_config';
|
||||
|
||||
export * from './application';
|
||||
|
||||
export * from 'change_detection/change_detector';
|
||||
export * from 'change_detection/watch_group';
|
||||
export * from 'change_detection/record';
|
||||
|
Reference in New Issue
Block a user