Skip to content

Commit 45c0758

Browse files
feat(UIRouter): update to ng2 beta.17
chore(uiView): switch from DynamicComponentLoader to ComponentResolver chore(uiView): switch from Injector to ReflectiveInjector
1 parent 61b5f1e commit 45c0758

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"license": "MIT",
4343
"devDependencies": {
44-
"angular2": "^2.0.0-beta.13",
44+
"angular2": "^2.0.0-beta.17",
4545
"babel-core": "^5.8.14",
4646
"conventional-changelog": "^1.1.0",
4747
"conventional-changelog-cli": "^1.1.1",
@@ -83,5 +83,9 @@
8383
"webpack-dev-server": "1.x",
8484
"yargs": "^4.2.0",
8585
"zone.js": "^0.6.6"
86+
},
87+
"dependencies": {
88+
"rxjs": "^5.0.0-beta.6",
89+
"zone.js": "^0.6.12"
8690
}
8791
}

src/ng2/uiView.ts

+28-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/** @module ng2_directives */ /** */
2-
import {Component, ElementRef, DynamicComponentLoader} from 'angular2/core';
3-
import {Injector} from "angular2/core";
2+
import {
3+
Component, ComponentResolver, ComponentFactory,
4+
ViewContainerRef, ReflectiveInjector
5+
} from 'angular2/core';
46
import {provide} from "angular2/core";
57
import {Input} from "angular2/core";
68
import {ComponentRef} from "angular2/core";
@@ -17,14 +19,6 @@ import {Ng2ViewConfig} from "./viewsBuilder";
1719
/** @hidden */
1820
let id = 0;
1921

20-
const getProviders = (injector) => {
21-
let providers = [], parentInj = injector.parent;
22-
for (let i = 0; i < parentInj._proto.numberOfProviders; i++) {
23-
providers.push(parentInj._proto.getProviderAtIndex(i));
24-
}
25-
return providers;
26-
};
27-
2822
// These are provide()d as the string UiView.PARENT_INJECT
2923
export interface ParentUiViewInject {
3024
context: ViewContext;
@@ -81,13 +75,13 @@ export interface ParentUiViewInject {
8175
*/
8276
@Component({
8377
selector: 'ui-view, [ui-view]',
84-
styles: [`
85-
.done-true {
86-
text-decoration: line-through;
87-
color: grey;
88-
}`
89-
],
90-
template: `<div #content></div>`,
78+
template: ''
79+
// styles: [`
80+
// .done-true {
81+
// text-decoration: line-through;
82+
// color: grey;
83+
// }`
84+
// ],
9185
// template: `
9286
// <div style="padding: 1em; border: 1px solid lightgrey;">
9387
//
@@ -100,7 +94,7 @@ export interface ParentUiViewInject {
10094
// </div>`
10195
})
10296
export class UiView {
103-
@Input() name: string;
97+
@Input('name') name: string;
10498
@Input('ui-view') set _name(val) { this.name = val; }
10599
componentRef: ComponentRef;
106100
deregister: Function;
@@ -111,9 +105,8 @@ export class UiView {
111105
constructor(
112106
public router: UIRouter,
113107
@Inject(UiView.PARENT_INJECT) public parent: ParentUiViewInject,
114-
public dcl: DynamicComponentLoader,
115-
public elementRef: ElementRef,
116-
public injector: Injector
108+
public compResolver: ComponentResolver,
109+
public viewContainerRef: ViewContainerRef
117110
) { }
118111

119112
ngOnInit() {
@@ -134,7 +127,7 @@ export class UiView {
134127
}
135128

136129
disposeLast() {
137-
if (this.componentRef) this.componentRef.dispose();
130+
if (this.componentRef) this.componentRef.destroy();
138131
this.componentRef = null;
139132
}
140133

@@ -147,7 +140,7 @@ export class UiView {
147140
if (!config) return this.disposeLast();
148141
if (!(config instanceof Ng2ViewConfig)) return;
149142

150-
let {uiViewData, injector, dcl, elementRef} = this;
143+
let uiViewData = this.uiViewData;
151144
let viewDecl = <Ng2ViewDeclaration> config.viewDecl;
152145

153146
// The "new" viewconfig is already applied, so exit early
@@ -159,30 +152,32 @@ export class UiView {
159152
// The config may be undefined if there is nothing state currently targeting this UiView.
160153
if (!config) return;
161154

162-
// Do some magic
155+
// Map resolves to "useValue providers"
163156
let rc = config.node.resolveContext;
164157
let resolvables = rc.getResolvables();
165158
let rawProviders = Object.keys(resolvables).map(key => provide(key, { useValue: resolvables[key].data }));
166159
rawProviders.push(provide(UiView.PARENT_INJECT, { useValue: { context: config.viewDecl.$context, fqn: uiViewData.fqn } }));
167-
let providers = Injector.resolve(rawProviders);
168160

169-
let exclusions = [UiView.PARENT_INJECT];
170-
providers = getProviders(injector).filter(x => exclusions.indexOf(x.key.displayName) === -1).concat(providers);
161+
// Get the component class from the view declaration. TODO: allow promises?
162+
let componentType = <Type> viewDecl.component;
171163

172-
let component = <Type> viewDecl.component;
173-
dcl.loadIntoLocation(component, elementRef, "content", providers).then(ref => {
174-
this.componentRef = ref;
164+
let createComponent = (factory: ComponentFactory) => {
165+
let parentInjector = this.viewContainerRef.injector;
166+
let childInjector = ReflectiveInjector.resolveAndCreate(rawProviders, parentInjector);
167+
let ref = this.componentRef = this.viewContainerRef.createComponent(factory, undefined, childInjector);
175168

176169
// TODO: wire uiCanExit and uiOnParamsChanged callbacks
177170

178-
// Set resolve data to matching @Input("prop")
179-
let inputs = ng2ComponentInputs(component);
171+
// Supply resolve data to matching @Input('prop') or inputs: ['prop']
172+
let inputs = ng2ComponentInputs(componentType);
180173
let bindings = viewDecl['bindings'] || {};
181174

182175
inputs.map(tuple => ({ prop: tuple.prop, resolve: bindings[tuple.prop] || tuple.resolve }))
183176
.filter(tuple => resolvables[tuple.resolve] !== undefined)
184177
.forEach(tuple => { ref.instance[tuple.prop] = resolvables[tuple.resolve].data });
185-
});
178+
};
179+
180+
this.compResolver.resolveComponent(componentType).then(createComponent);
186181
}
187182
}
188183

0 commit comments

Comments
 (0)