Skip to content

Commit 6f8fef4

Browse files
committed
feat(bootstrap): changed bootstrap to return ComponentRef
1 parent e295940 commit 6f8fef4

File tree

6 files changed

+54
-64
lines changed

6 files changed

+54
-64
lines changed

modules/angular2/src/core/application.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {ProtoViewFactory} from 'angular2/src/core/compiler/proto_view_factory';
3232
import {Renderer} from 'angular2/src/render/api';
3333
import {DirectDomRenderer} from 'angular2/src/render/dom/direct_dom_renderer';
3434
import * as rc from 'angular2/src/render/dom/compiler/compiler';
35+
import {ComponentRef} from 'angular2/src/core/compiler/element_injector';
3536
import * as rvf from 'angular2/src/render/dom/view/view_factory';
3637

3738
import {
@@ -244,7 +245,7 @@ function _createVmZone(givenReporter:Function): VmTurnZone {
244245
*/
245246
export function bootstrap(appComponentType: Type,
246247
componentInjectableBindings: List<Binding> = null,
247-
errorReporter: Function = null): Promise<Injector> {
248+
errorReporter: Function = null): Promise<ComponentRef> {
248249
BrowserDomAdapter.makeCurrent();
249250
var bootstrapProcess = PromiseWrapper.completer();
250251

@@ -255,14 +256,15 @@ export function bootstrap(appComponentType: Type,
255256

256257
var appInjector = _createAppInjector(appComponentType, componentInjectableBindings, zone);
257258

258-
PromiseWrapper.then(appInjector.asyncGet(appChangeDetectorToken),
259-
(appChangeDetector) => {
259+
PromiseWrapper.then(appInjector.asyncGet(appComponentRefToken),
260+
(componentRef) => {
261+
var appChangeDetector = componentRef.hostView.changeDetector;
260262
// retrieve life cycle: may have already been created if injected in root component
261263
var lc = appInjector.get(LifeCycle);
262264
lc.registerWith(zone, appChangeDetector);
263265
lc.tick(); //the first tick that will bootstrap the app
264266

265-
bootstrapProcess.resolve(appInjector);
267+
bootstrapProcess.resolve(componentRef);
266268
},
267269

268270
(err) => {

modules/angular2/src/core/compiler/dynamic_component_loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class DynamicComponentLoader {
7979
}
8080

8181
_componentAppInjector(location, injector, services) {
82-
var inj = isPresent(injector) ? injector : location.elementInjector.getLightDomAppInjector();
82+
var inj = isPresent(injector) ? injector : location.injector;
8383
return isPresent(services) ? inj.createChild(services) : inj;
8484
}
8585

modules/angular2/src/core/compiler/element_injector.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ export class ElementRef {
2727
}
2828

2929
get hostView() {
30-
return this.elementInjector.getHostView();
30+
return this.elementInjector._preBuiltObjects.view;
31+
}
32+
33+
get injector() {
34+
return this.elementInjector._lightDomAppInjector;
3135
}
3236

3337
get boundElementIndex() {
34-
return this.elementInjector.getBoundElementIndex();
38+
return this.elementInjector._proto.index;
3539
}
3640
}
3741

@@ -608,21 +612,11 @@ export class ElementInjector extends TreeNode {
608612
return this._getDirectiveByKeyId(Key.get(type).id) !== _undefined;
609613
}
610614

611-
hasPreBuiltObject(type:Type):boolean {
612-
var pb = this._getPreBuiltObjectByKeyId(Key.get(type).id);
613-
return pb !== _undefined && isPresent(pb);
614-
}
615-
616615
/** Gets the NgElement associated with this ElementInjector */
617616
getNgElement() {
618617
return this._preBuiltObjects.element;
619618
}
620619

621-
/** Gets the View associated with this ElementInjector */
622-
getHostView() {
623-
return this._preBuiltObjects.view;
624-
}
625-
626620
getComponent() {
627621
if (this._proto._binding0IsComponent) {
628622
return this._obj0;
@@ -635,10 +629,6 @@ export class ElementInjector extends TreeNode {
635629
return this._dynamicallyCreatedComponent;
636630
}
637631

638-
getLightDomAppInjector() {
639-
return this._lightDomAppInjector;
640-
}
641-
642632
directParent(): ElementInjector {
643633
return this._proto.distanceToParent < 2 ? this.parent : null;
644634
}
@@ -699,10 +689,6 @@ export class ElementInjector extends TreeNode {
699689
return obj;
700690
}
701691

702-
getBoundElementIndex() {
703-
return this._proto.index;
704-
}
705-
706692
_getByDependency(dep:DirectiveDependency, requestor:Key) {
707693
if (isPresent(dep.eventEmitterName)) return this._buildEventEmitter(dep);
708694
if (isPresent(dep.propSetterName)) return this._buildPropSetter(dep);

modules/angular2/test/core/application_spec.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -88,107 +88,107 @@ export function main() {
8888

8989
describe('bootstrap factory method', () => {
9090
it('should throw if no View found', inject([AsyncTestCompleter], (async) => {
91-
var injectorPromise = bootstrap(HelloRootMissingTemplate, testBindings, (e,t) => {throw e;});
92-
PromiseWrapper.then(injectorPromise, null, (reason) => {
91+
var refPromise = bootstrap(HelloRootMissingTemplate, testBindings, (e,t) => {throw e;});
92+
PromiseWrapper.then(refPromise, null, (reason) => {
9393
expect(reason.message).toContain('No template found for HelloRootMissingTemplate');
9494
async.done();
9595
});
9696
}));
9797

9898
it('should throw if bootstrapped Directive is not a Component', inject([AsyncTestCompleter], (async) => {
99-
var injectorPromise = bootstrap(HelloRootDirectiveIsNotCmp, testBindings, (e,t) => {throw e;});
100-
PromiseWrapper.then(injectorPromise, null, (reason) => {
99+
var refPromise = bootstrap(HelloRootDirectiveIsNotCmp, testBindings, (e,t) => {throw e;});
100+
PromiseWrapper.then(refPromise, null, (reason) => {
101101
expect(reason.message).toContain(`Could not load 'HelloRootDirectiveIsNotCmp' because it is not a component.`);
102102
async.done();
103103
});
104104
}));
105105

106106
it('should throw if no element is found', inject([AsyncTestCompleter], (async) => {
107-
var injectorPromise = bootstrap(HelloRootCmp, [], (e,t) => {throw e;});
108-
PromiseWrapper.then(injectorPromise, null, (reason) => {
107+
var refPromise = bootstrap(HelloRootCmp, [], (e,t) => {throw e;});
108+
PromiseWrapper.then(refPromise, null, (reason) => {
109109
expect(reason.message).toContain(
110110
'The app selector "hello-app" did not match any elements');
111111
async.done();
112112
});
113113
}));
114114

115115
it('should create an injector promise', () => {
116-
var injectorPromise = bootstrap(HelloRootCmp, testBindings);
117-
expect(injectorPromise).not.toBe(null);
116+
var refPromise = bootstrap(HelloRootCmp, testBindings);
117+
expect(refPromise).not.toBe(null);
118118
});
119119

120120
it('should resolve an injector promise and contain bindings', inject([AsyncTestCompleter], (async) => {
121-
var injectorPromise = bootstrap(HelloRootCmp, testBindings);
122-
injectorPromise.then((injector) => {
123-
expect(injector.get(appElementToken)).toBe(el);
121+
var refPromise = bootstrap(HelloRootCmp, testBindings);
122+
refPromise.then((ref) => {
123+
expect(ref.injector.get(appElementToken)).toBe(el);
124124
async.done();
125125
});
126126
}));
127127

128128
it('should provide the application component in the injector', inject([AsyncTestCompleter], (async) => {
129-
var injectorPromise = bootstrap(HelloRootCmp, testBindings);
130-
injectorPromise.then((injector) => {
131-
expect(injector.get(HelloRootCmp)).toBeAnInstanceOf(HelloRootCmp);
129+
var refPromise = bootstrap(HelloRootCmp, testBindings);
130+
refPromise.then((ref) => {
131+
expect(ref.injector.get(HelloRootCmp)).toBeAnInstanceOf(HelloRootCmp);
132132
async.done();
133133
});
134134
}));
135135

136136
it('should display hello world', inject([AsyncTestCompleter], (async) => {
137-
var injectorPromise = bootstrap(HelloRootCmp, testBindings);
138-
injectorPromise.then((injector) => {
139-
expect(injector.get(appElementToken)).toHaveText('hello world!');
137+
var refPromise = bootstrap(HelloRootCmp, testBindings);
138+
refPromise.then((ref) => {
139+
expect(ref.injector.get(appElementToken)).toHaveText('hello world!');
140140
async.done();
141141
});
142142
}));
143143

144144
it('should support multiple calls to bootstrap', inject([AsyncTestCompleter], (async) => {
145-
var injectorPromise1 = bootstrap(HelloRootCmp, testBindings);
146-
var injectorPromise2 = bootstrap(HelloRootCmp2, testBindings);
147-
PromiseWrapper.all([injectorPromise1, injectorPromise2]).then((injectors) => {
148-
expect(injectors[0].get(appElementToken)).toHaveText('hello world!');
149-
expect(injectors[1].get(appElementToken)).toHaveText('hello world, again!');
145+
var refPromise1 = bootstrap(HelloRootCmp, testBindings);
146+
var refPromise2 = bootstrap(HelloRootCmp2, testBindings);
147+
PromiseWrapper.all([refPromise1, refPromise2]).then((refs) => {
148+
expect(refs[0].injector.get(appElementToken)).toHaveText('hello world!');
149+
expect(refs[1].injector.get(appElementToken)).toHaveText('hello world, again!');
150150
async.done();
151151
});
152152
}));
153153

154154
it("should make the provided bindings available to the application component", inject([AsyncTestCompleter], (async) => {
155-
var injectorPromise = bootstrap(HelloRootCmp3, [
155+
var refPromise = bootstrap(HelloRootCmp3, [
156156
testBindings,
157157
bind("appBinding").toValue("BoundValue")
158158
]);
159159

160-
injectorPromise.then((injector) => {
161-
expect(injector.get(HelloRootCmp3).appBinding).toEqual("BoundValue");
160+
refPromise.then((ref) => {
161+
expect(ref.injector.get(HelloRootCmp3).appBinding).toEqual("BoundValue");
162162
async.done();
163163
});
164164
}));
165165

166166
it("should avoid cyclic dependencies when root component requires Lifecycle through DI", inject([AsyncTestCompleter], (async) => {
167-
var injectorPromise = bootstrap(HelloRootCmp4, testBindings);
167+
var refPromise = bootstrap(HelloRootCmp4, testBindings);
168168

169-
injectorPromise.then((injector) => {
170-
expect(injector.get(HelloRootCmp4).lc).toBe(injector.get(LifeCycle));
169+
refPromise.then((ref) => {
170+
expect(ref.injector.get(HelloRootCmp4).lc).toBe(ref.injector.get(LifeCycle));
171171
async.done();
172172
});
173173
}));
174174

175175
it("should support shadow dom content tag", inject([AsyncTestCompleter], (async) => {
176-
var injectorPromise = bootstrap(HelloRootCmpContent, testBindings);
177-
injectorPromise.then((injector) => {
178-
expect(injector.get(appElementToken)).toHaveText('before: loading after: done');
176+
var refPromise = bootstrap(HelloRootCmpContent, testBindings);
177+
refPromise.then((ref) => {
178+
expect(ref.injector.get(appElementToken)).toHaveText('before: loading after: done');
179179
async.done();
180180
});
181181
}));
182182

183183
it('should register each application with the testability registry', inject([AsyncTestCompleter], (async) => {
184-
var injectorPromise1 = bootstrap(HelloRootCmp, testBindings);
185-
var injectorPromise2 = bootstrap(HelloRootCmp2, testBindings);
184+
var refPromise1 = bootstrap(HelloRootCmp, testBindings);
185+
var refPromise2 = bootstrap(HelloRootCmp2, testBindings);
186186

187-
PromiseWrapper.all([injectorPromise1, injectorPromise2]).then((injectors) => {
188-
var registry = injectors[0].get(TestabilityRegistry);
187+
PromiseWrapper.all([refPromise1, refPromise2]).then((refs) => {
188+
var registry = refs[0].injector.get(TestabilityRegistry);
189189
PromiseWrapper.all([
190-
injectors[0].asyncGet(Testability),
191-
injectors[1].asyncGet(Testability)]).then((testabilities) => {
190+
refs[0].injector.asyncGet(Testability),
191+
refs[1].injector.asyncGet(Testability)]).then((testabilities) => {
192192
expect(registry.findTestabilityInTree(el)).toEqual(testabilities[0]);
193193
expect(registry.findTestabilityInTree(el2)).toEqual(testabilities[1]);
194194
async.done();

modules/benchmarks/src/largetable/largetable_benchmark.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ export function main() {
142142
function noop() {}
143143

144144
function initNg2() {
145-
bootstrap(AppComponent, _createBindings()).then((injector) => {
145+
bootstrap(AppComponent, _createBindings()).then((ref) => {
146+
var injector = ref.injector;
146147
app = injector.get(AppComponent);
147148
lifecycle = injector.get(LifeCycle);
148149
bindAction('#ng2DestroyDom', ng2DestroyDom);

modules/benchmarks/src/tree/tree_benchmark.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ export function main() {
120120
function noop() {}
121121

122122
function initNg2() {
123-
bootstrap(AppComponent, createBindings()).then((injector) => {
123+
bootstrap(AppComponent, createBindings()).then((ref) => {
124+
var injector = ref.injector;
124125
lifeCycle = injector.get(LifeCycle);
125126

126127
app = injector.get(AppComponent);

0 commit comments

Comments
 (0)