2015-02-05 13:08:05 -08:00
|
|
|
import {Injector, bind, OpaqueToken} from 'angular2/di';
|
2015-03-18 18:30:45 -07:00
|
|
|
import {Type, isBlank, isPresent, BaseException, assertionsEnabled, print, stringify} from 'angular2/src/facade/lang';
|
2015-02-27 14:50:06 -08:00
|
|
|
import {BrowserDomAdapter} from 'angular2/src/dom/browser_adapter';
|
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2014-12-02 13:21:39 -08:00
|
|
|
import {Compiler, CompilerCache} from './compiler/compiler';
|
2014-11-07 14:30:04 -08:00
|
|
|
import {ProtoView} from './compiler/view';
|
2015-02-05 13:08:05 -08:00
|
|
|
import {Reflector, reflector} from 'angular2/src/reflection/reflection';
|
|
|
|
import {Parser, Lexer, ChangeDetection, dynamicChangeDetection, jitChangeDetection} from 'angular2/change_detection';
|
2015-02-16 14:35:27 +01:00
|
|
|
import {ExceptionHandler} from './exception_handler';
|
2015-04-02 14:40:49 -07:00
|
|
|
import {TemplateLoader} from 'angular2/src/render/dom/compiler/template_loader';
|
2015-02-12 14:44:59 +01:00
|
|
|
import {TemplateResolver} from './compiler/template_resolver';
|
2014-11-20 12:07:48 -08:00
|
|
|
import {DirectiveMetadataReader} from './compiler/directive_metadata_reader';
|
2015-04-02 14:40:49 -07:00
|
|
|
import {DirectiveBinding} from './compiler/element_injector';
|
2015-02-05 13:08:05 -08:00
|
|
|
import {List, ListWrapper} from 'angular2/src/facade/collection';
|
2015-02-11 21:04:52 -08:00
|
|
|
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
|
2015-02-05 13:08:05 -08:00
|
|
|
import {VmTurnZone} from 'angular2/src/core/zone/vm_turn_zone';
|
|
|
|
import {LifeCycle} from 'angular2/src/core/life_cycle/life_cycle';
|
2015-03-10 12:30:50 -07:00
|
|
|
import {ShadowDomStrategy, NativeShadowDomStrategy, EmulatedUnscopedShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy';
|
2015-04-02 09:52:00 -07:00
|
|
|
import {XHR} from 'angular2/src/services/xhr';
|
|
|
|
import {XHRImpl} from 'angular2/src/services/xhr_impl';
|
|
|
|
import {EventManager, DomEventsPlugin} from 'angular2/src/render/dom/events/event_manager';
|
|
|
|
import {HammerGesturesPlugin} from 'angular2/src/render/dom/events/hammer_gestures';
|
2015-02-11 21:04:52 -08:00
|
|
|
import {Binding} from 'angular2/src/di/binding';
|
2015-02-24 16:05:45 +01:00
|
|
|
import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mapper';
|
2015-04-02 09:52:00 -07:00
|
|
|
import {UrlResolver} from 'angular2/src/services/url_resolver';
|
2015-04-02 14:40:49 -07:00
|
|
|
import {StyleUrlResolver} from 'angular2/src/render/dom/shadow_dom/style_url_resolver';
|
|
|
|
import {StyleInliner} from 'angular2/src/render/dom/shadow_dom/style_inliner';
|
2015-03-18 18:30:45 -07:00
|
|
|
import {Component} from 'angular2/src/core/annotations/annotations';
|
2015-03-24 10:19:05 +01:00
|
|
|
import {PrivateComponentLoader} from 'angular2/src/core/compiler/private_component_loader';
|
2015-03-23 16:46:18 -07:00
|
|
|
import {TestabilityRegistry, Testability} from 'angular2/src/core/testability/testability';
|
2014-11-07 14:30:04 -08:00
|
|
|
|
|
|
|
var _rootInjector: Injector;
|
|
|
|
|
|
|
|
// Contains everything that is safe to share between applications.
|
2014-11-20 12:07:48 -08:00
|
|
|
var _rootBindings = [
|
2015-03-23 16:46:18 -07:00
|
|
|
bind(Reflector).toValue(reflector),
|
|
|
|
TestabilityRegistry
|
2014-11-20 12:07:48 -08:00
|
|
|
];
|
2014-11-07 14:30:04 -08:00
|
|
|
|
2014-11-25 18:52:42 -08:00
|
|
|
export var appViewToken = new OpaqueToken('AppView');
|
2015-01-14 13:51:16 -08:00
|
|
|
export var appChangeDetectorToken = new OpaqueToken('AppChangeDetector');
|
2014-11-25 18:52:42 -08:00
|
|
|
export var appElementToken = new OpaqueToken('AppElement');
|
|
|
|
export var appComponentAnnotatedTypeToken = new OpaqueToken('AppComponentAnnotatedType');
|
|
|
|
export var appDocumentToken = new OpaqueToken('AppDocument');
|
2014-11-07 14:30:04 -08:00
|
|
|
|
2015-02-11 21:04:52 -08:00
|
|
|
function _injectorBindings(appComponentType): List<Binding> {
|
2014-11-07 14:30:04 -08:00
|
|
|
return [
|
2015-01-08 09:11:33 -08:00
|
|
|
bind(appDocumentToken).toValue(DOM.defaultDoc()),
|
2014-11-20 12:07:48 -08:00
|
|
|
bind(appComponentAnnotatedTypeToken).toFactory((reader) => {
|
2015-03-11 16:48:57 -07:00
|
|
|
// TODO(rado): investigate whether to support bindings on root component.
|
2014-12-23 10:45:20 -08:00
|
|
|
return reader.read(appComponentType);
|
2014-11-20 12:07:48 -08:00
|
|
|
}, [DirectiveMetadataReader]),
|
2014-11-07 14:30:04 -08:00
|
|
|
|
|
|
|
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]),
|
2015-01-21 12:05:52 -08:00
|
|
|
bind(appViewToken).toAsyncFactory((changeDetection, compiler, injector, appElement,
|
2015-03-23 16:46:18 -07:00
|
|
|
appComponentAnnotatedType, strategy, eventManager, testability, registry) => {
|
|
|
|
|
|
|
|
// We need to do this here to ensure that we create Testability and
|
|
|
|
// it's ready on the window for users.
|
|
|
|
registry.registerApplication(appElement, testability);
|
2015-03-18 18:30:45 -07:00
|
|
|
var annotation = appComponentAnnotatedType.annotation;
|
|
|
|
if(!isBlank(annotation) && !(annotation instanceof Component)) {
|
|
|
|
var type = appComponentAnnotatedType.type;
|
|
|
|
throw new BaseException(`Only Components can be bootstrapped; ` +
|
|
|
|
`Directive of ${stringify(type)} is not a Component`);
|
|
|
|
}
|
2015-02-12 14:44:59 +01:00
|
|
|
return compiler.compile(appComponentAnnotatedType.type).then(
|
2014-11-07 14:30:04 -08:00
|
|
|
(protoView) => {
|
2015-01-30 09:43:21 +01:00
|
|
|
var appProtoView = ProtoView.createRootProtoView(protoView, appElement,
|
2015-04-02 14:40:49 -07:00
|
|
|
DirectiveBinding.createFromType(appComponentAnnotatedType.type, appComponentAnnotatedType.annotation),
|
|
|
|
changeDetection.createProtoChangeDetector('root'),
|
2015-01-30 09:43:21 +01:00
|
|
|
strategy);
|
2014-11-07 14:30:04 -08:00
|
|
|
// The light Dom of the app element is not considered part of
|
|
|
|
// the angular application. Thus the context and lightDomInjector are
|
|
|
|
// empty.
|
2015-03-11 19:57:21 +01:00
|
|
|
var view = appProtoView.instantiate(null, eventManager);
|
2015-03-11 21:11:39 -07:00
|
|
|
view.hydrate(injector, null, null, new Object(), null);
|
2014-12-01 18:41:55 -08:00
|
|
|
return view;
|
2014-11-07 14:30:04 -08:00
|
|
|
});
|
2015-01-30 09:43:21 +01:00
|
|
|
}, [ChangeDetection, Compiler, Injector, appElementToken, appComponentAnnotatedTypeToken,
|
2015-03-23 16:46:18 -07:00
|
|
|
ShadowDomStrategy, EventManager, Testability, TestabilityRegistry]),
|
2014-11-07 14:30:04 -08:00
|
|
|
|
2015-01-14 13:51:16 -08:00
|
|
|
bind(appChangeDetectorToken).toFactory((rootView) => rootView.changeDetector,
|
2014-11-07 14:30:04 -08:00
|
|
|
[appViewToken]),
|
2014-12-08 10:54:43 -08:00
|
|
|
bind(appComponentType).toFactory((rootView) => rootView.elementInjectors[0].getComponent(),
|
2014-12-10 10:48:17 -08:00
|
|
|
[appViewToken]),
|
2015-02-16 14:55:00 +01:00
|
|
|
bind(LifeCycle).toFactory((exceptionHandler) => new LifeCycle(exceptionHandler, null, assertionsEnabled()),[ExceptionHandler]),
|
2015-02-09 15:11:31 +01:00
|
|
|
bind(EventManager).toFactory((zone) => {
|
2015-02-19 19:10:16 -08:00
|
|
|
var plugins = [new HammerGesturesPlugin(), new DomEventsPlugin()];
|
2015-02-09 15:11:31 +01:00
|
|
|
return new EventManager(plugins, zone);
|
|
|
|
}, [VmTurnZone]),
|
2015-03-10 12:30:50 -07:00
|
|
|
bind(ShadowDomStrategy).toFactory(
|
|
|
|
(styleUrlResolver, doc) => new EmulatedUnscopedShadowDomStrategy(styleUrlResolver, doc.head),
|
|
|
|
[StyleUrlResolver, appDocumentToken]),
|
2015-02-16 10:21:54 +01:00
|
|
|
Compiler,
|
|
|
|
CompilerCache,
|
|
|
|
TemplateResolver,
|
|
|
|
bind(ChangeDetection).toValue(dynamicChangeDetection),
|
|
|
|
TemplateLoader,
|
|
|
|
DirectiveMetadataReader,
|
|
|
|
Parser,
|
|
|
|
Lexer,
|
2015-02-16 14:35:27 +01:00
|
|
|
ExceptionHandler,
|
2015-02-16 10:21:54 +01:00
|
|
|
bind(XHR).toValue(new XHRImpl()),
|
2015-02-24 16:05:45 +01:00
|
|
|
ComponentUrlMapper,
|
|
|
|
UrlResolver,
|
|
|
|
StyleUrlResolver,
|
|
|
|
StyleInliner,
|
2015-03-24 10:19:05 +01:00
|
|
|
PrivateComponentLoader,
|
2015-03-23 16:46:18 -07:00
|
|
|
Testability,
|
2014-11-07 14:30:04 -08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-02-11 21:04:52 -08:00
|
|
|
function _createVmZone(givenReporter:Function): VmTurnZone {
|
2014-12-11 11:36:05 -08:00
|
|
|
var defaultErrorReporter = (exception, stackTrace) => {
|
|
|
|
var longStackTrace = ListWrapper.join(stackTrace, "\n\n-----async gap-----\n");
|
|
|
|
print(`${exception}\n\n${longStackTrace}`);
|
|
|
|
throw exception;
|
|
|
|
};
|
|
|
|
|
|
|
|
var reporter = isPresent(givenReporter) ? givenReporter : defaultErrorReporter;
|
|
|
|
|
|
|
|
var zone = new VmTurnZone({enableLongStackTrace: assertionsEnabled()});
|
|
|
|
zone.initCallbacks({onErrorHandler: reporter});
|
|
|
|
return zone;
|
|
|
|
}
|
|
|
|
|
2015-03-13 21:54:19 +00:00
|
|
|
/**
|
2015-03-17 19:22:13 +00:00
|
|
|
* Bootstrapping for Angular applications.
|
|
|
|
*
|
|
|
|
* You instantiate an Angular application by explicitly specifying a component to use as the root component for your
|
2015-03-13 21:54:19 +00:00
|
|
|
* application via the `bootstrap()` method.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-13 21:54:19 +00:00
|
|
|
* ## Simple Example
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-13 21:54:19 +00:00
|
|
|
* Assuming this `index.html`:
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
* ```html
|
|
|
|
* <html>
|
|
|
|
* <!-- load Angular script tags here. -->
|
|
|
|
* <body>
|
|
|
|
* <my-app>loading...</my-app>
|
|
|
|
* </body>
|
|
|
|
* </html>
|
|
|
|
* ```
|
|
|
|
*
|
2015-03-13 21:54:19 +00:00
|
|
|
* An application is bootstrapped inside an existing browser DOM, typically `index.html`. Unlike Angular 1, Angular 2
|
|
|
|
* does not compile/process bindings in `index.html`. This is mainly for security reasons, as well as architectural
|
2015-03-15 20:26:52 -07:00
|
|
|
* changes in Angular 2. This means that `index.html` can safely be processed using server-side technologies such as
|
2015-03-30 17:19:27 -07:00
|
|
|
* bindings. Bindings can thus use double-curly `{{ syntax }}` without collision from Angular 2 component double-curly
|
|
|
|
* `{{ syntax }}`.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-13 21:54:19 +00:00
|
|
|
* We can use this script code:
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component({
|
|
|
|
* selector: 'my-app'
|
|
|
|
* })
|
|
|
|
* @Template({
|
2015-03-15 20:26:52 -07:00
|
|
|
* inline: 'Hello {{ name }}!'
|
2015-03-17 19:22:13 +00:00
|
|
|
* })
|
|
|
|
* class MyApp {
|
|
|
|
* name:string;
|
|
|
|
*
|
|
|
|
* constructor() {
|
|
|
|
* this.name = 'World';
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* main() {
|
2015-03-15 20:26:52 -07:00
|
|
|
* return bootstrap(MyApp);
|
2015-03-17 19:22:13 +00:00
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* When the app developer invokes `bootstrap()` with the root component `MyApp` as its argument, Angular performs the
|
2015-03-13 21:54:19 +00:00
|
|
|
* following tasks:
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
* 1. It uses the component's `selector` property to locate the DOM element which needs to be upgraded into
|
2015-03-13 21:54:19 +00:00
|
|
|
* the angular component.
|
2015-03-17 19:22:13 +00:00
|
|
|
* 2. It creates a new child injector (from the primordial injector) and configures the injector with the component's
|
|
|
|
* `services`. Optionally, you can also override the injector configuration for an app by invoking
|
2015-03-13 21:54:19 +00:00
|
|
|
* `bootstrap` with the `componentServiceBindings` argument.
|
2015-03-17 19:22:13 +00:00
|
|
|
* 3. It creates a new [Zone] and connects it to the angular application's change detection domain instance.
|
2015-03-13 21:54:19 +00:00
|
|
|
* 4. It creates a shadow DOM on the selected component's host element and loads the template into it.
|
|
|
|
* 5. It instantiates the specified component.
|
|
|
|
* 6. Finally, Angular performs change detection to apply the initial data bindings for the application.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
*
|
2015-03-13 21:54:19 +00:00
|
|
|
* ## Instantiating Multiple Applications on a Single Page
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-13 21:54:19 +00:00
|
|
|
* There are two ways to do this.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
*
|
2015-03-13 21:54:19 +00:00
|
|
|
* ### Isolated Applications
|
|
|
|
*
|
2015-03-17 19:22:13 +00:00
|
|
|
* Angular creates a new application each time that the `bootstrap()` method is invoked. When multiple applications
|
|
|
|
* are created for a page, Angular treats each application as independent within an isolated change detection and
|
|
|
|
* [Zone] domain. If you need to share data between applications, use the strategy described in the next
|
|
|
|
* section, "Applications That Share Change Detection."
|
|
|
|
*
|
|
|
|
*
|
2015-03-13 21:54:19 +00:00
|
|
|
* ### Applications That Share Change Detection
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
* If you need to bootstrap multiple applications that share common data, the applications must share a common
|
2015-03-13 21:54:19 +00:00
|
|
|
* change detection and zone. To do that, create a meta-component that lists the application components in its template.
|
2015-03-30 17:19:27 -07:00
|
|
|
* By only invoking the `bootstrap()` method once, with the meta-component as its argument, you ensure that only a
|
|
|
|
* single change detection zone is created and therefore data can be shared across the applications.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
*
|
2015-03-13 21:54:19 +00:00
|
|
|
* ## Primordial Injector
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
* When working within a browser window, there are many singleton resources: cookies, title, location, and others.
|
|
|
|
* Angular services that represent these resources must likewise be shared across all Angular applications that
|
2015-03-13 21:54:19 +00:00
|
|
|
* occupy the same browser window. For this reason, Angular creates exactly one global primordial injector which stores
|
2015-03-17 19:22:13 +00:00
|
|
|
* all shared services, and each angular application injector has the primordial injector as its parent.
|
|
|
|
*
|
|
|
|
* Each application has its own private injector as well. When there are multiple applications on a page, Angular treats
|
2015-03-13 21:54:19 +00:00
|
|
|
* each application injector's services as private to that application.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
*
|
2015-03-13 21:54:19 +00:00
|
|
|
* # API
|
|
|
|
* - [appComponentType]: The root component which should act as the application. This is a reference to a [Type]
|
|
|
|
* which is annotated with `@Component(...)`.
|
2015-03-17 19:22:13 +00:00
|
|
|
* - [componentServiceBindings]: An additional set of bindings that can be added to the [Component.services] to
|
2015-03-13 21:54:19 +00:00
|
|
|
* override default injection behavior.
|
|
|
|
* - [errorReporter]: `function(exception:any, stackTrace:string)` a default error reporter for unhandled exceptions.
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
2015-03-15 20:26:52 -07:00
|
|
|
* Returns a [Promise] with the application`s private [Injector].
|
2015-03-17 19:22:13 +00:00
|
|
|
*
|
|
|
|
* @publicModule angular2/angular2
|
2015-03-13 21:54:19 +00:00
|
|
|
*/
|
2015-03-17 19:22:13 +00:00
|
|
|
export function bootstrap(appComponentType: Type,
|
|
|
|
componentServiceBindings: List<Binding>=null,
|
2015-03-13 21:54:19 +00:00
|
|
|
errorReporter: Function=null): Promise<Injector> {
|
2015-02-27 14:50:06 -08:00
|
|
|
BrowserDomAdapter.makeCurrent();
|
2014-12-11 11:36:05 -08:00
|
|
|
var bootstrapProcess = PromiseWrapper.completer();
|
|
|
|
|
2015-03-13 21:54:19 +00:00
|
|
|
var zone = _createVmZone(errorReporter);
|
2014-12-11 11:36:05 -08:00
|
|
|
zone.run(() => {
|
|
|
|
// TODO(rado): prepopulate template cache, so applications with only
|
|
|
|
// index.html and main.js are possible.
|
2014-12-05 18:30:45 -08:00
|
|
|
|
2015-03-13 21:54:19 +00:00
|
|
|
var appInjector = _createAppInjector(appComponentType, componentServiceBindings, zone);
|
2014-12-10 10:48:17 -08:00
|
|
|
|
2015-02-03 17:05:29 -08:00
|
|
|
PromiseWrapper.then(appInjector.asyncGet(appViewToken),
|
|
|
|
(rootView) => {
|
|
|
|
// retrieve life cycle: may have already been created if injected in root component
|
2015-02-09 15:11:31 +01:00
|
|
|
var lc=appInjector.get(LifeCycle);
|
2015-02-03 17:05:29 -08:00
|
|
|
lc.registerWith(zone, rootView.changeDetector);
|
2014-12-11 11:36:05 -08:00
|
|
|
lc.tick(); //the first tick that will bootstrap the app
|
|
|
|
|
2015-02-25 12:46:50 +01:00
|
|
|
bootstrapProcess.resolve(appInjector);
|
2014-12-11 11:36:05 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
(err) => {
|
|
|
|
bootstrapProcess.reject(err)
|
|
|
|
});
|
2014-11-07 14:30:04 -08:00
|
|
|
});
|
2014-12-11 11:36:05 -08:00
|
|
|
|
|
|
|
return bootstrapProcess.promise;
|
2014-11-07 14:30:04 -08:00
|
|
|
}
|
2015-01-08 09:11:33 -08:00
|
|
|
|
2015-02-11 21:04:52 -08:00
|
|
|
function _createAppInjector(appComponentType: Type, bindings: List<Binding>, zone: VmTurnZone): Injector {
|
2015-01-08 09:11:33 -08:00
|
|
|
if (isBlank(_rootInjector)) _rootInjector = new Injector(_rootBindings);
|
|
|
|
var mergedBindings = isPresent(bindings) ?
|
|
|
|
ListWrapper.concat(_injectorBindings(appComponentType), bindings) :
|
|
|
|
_injectorBindings(appComponentType);
|
2015-02-09 15:11:31 +01:00
|
|
|
ListWrapper.push(mergedBindings, bind(VmTurnZone).toValue(zone));
|
2015-01-08 09:11:33 -08:00
|
|
|
return _rootInjector.createChild(mergedBindings);
|
|
|
|
}
|