Skip to content

Commit

Permalink
feat(UIRouter): update to ng2 beta.17
Browse files Browse the repository at this point in the history
chore(uiView): switch from DynamicComponentLoader to ComponentResolver
chore(uiView): switch from Injector to ReflectiveInjector
  • Loading branch information
christopherthielen committed May 4, 2016
1 parent 61b5f1e commit 45c0758
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"license": "MIT",
"devDependencies": {
"angular2": "^2.0.0-beta.13",
"angular2": "^2.0.0-beta.17",
"babel-core": "^5.8.14",
"conventional-changelog": "^1.1.0",
"conventional-changelog-cli": "^1.1.1",
Expand Down Expand Up @@ -83,5 +83,9 @@
"webpack-dev-server": "1.x",
"yargs": "^4.2.0",
"zone.js": "^0.6.6"
},
"dependencies": {
"rxjs": "^5.0.0-beta.6",
"zone.js": "^0.6.12"
}
}
61 changes: 28 additions & 33 deletions src/ng2/uiView.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/** @module ng2_directives */ /** */
import {Component, ElementRef, DynamicComponentLoader} from 'angular2/core';
import {Injector} from "angular2/core";
import {
Component, ComponentResolver, ComponentFactory,
ViewContainerRef, ReflectiveInjector
} from 'angular2/core';
import {provide} from "angular2/core";
import {Input} from "angular2/core";
import {ComponentRef} from "angular2/core";
Expand All @@ -17,14 +19,6 @@ import {Ng2ViewConfig} from "./viewsBuilder";
/** @hidden */
let id = 0;

const getProviders = (injector) => {
let providers = [], parentInj = injector.parent;
for (let i = 0; i < parentInj._proto.numberOfProviders; i++) {
providers.push(parentInj._proto.getProviderAtIndex(i));
}
return providers;
};

// These are provide()d as the string UiView.PARENT_INJECT
export interface ParentUiViewInject {
context: ViewContext;
Expand Down Expand Up @@ -81,13 +75,13 @@ export interface ParentUiViewInject {
*/
@Component({
selector: 'ui-view, [ui-view]',
styles: [`
.done-true {
text-decoration: line-through;
color: grey;
}`
],
template: `<div #content></div>`,
template: ''
// styles: [`
// .done-true {
// text-decoration: line-through;
// color: grey;
// }`
// ],
// template: `
// <div style="padding: 1em; border: 1px solid lightgrey;">
//
Expand All @@ -100,7 +94,7 @@ export interface ParentUiViewInject {
// </div>`
})
export class UiView {
@Input() name: string;
@Input('name') name: string;
@Input('ui-view') set _name(val) { this.name = val; }
componentRef: ComponentRef;
deregister: Function;
Expand All @@ -111,9 +105,8 @@ export class UiView {
constructor(
public router: UIRouter,
@Inject(UiView.PARENT_INJECT) public parent: ParentUiViewInject,
public dcl: DynamicComponentLoader,
public elementRef: ElementRef,
public injector: Injector
public compResolver: ComponentResolver,
public viewContainerRef: ViewContainerRef
) { }

ngOnInit() {
Expand All @@ -134,7 +127,7 @@ export class UiView {
}

disposeLast() {
if (this.componentRef) this.componentRef.dispose();
if (this.componentRef) this.componentRef.destroy();
this.componentRef = null;
}

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

let {uiViewData, injector, dcl, elementRef} = this;
let uiViewData = this.uiViewData;
let viewDecl = <Ng2ViewDeclaration> config.viewDecl;

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

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

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

This comment has been minimized.

Copy link
@christopherthielen

christopherthielen May 17, 2016

Author Contributor

switch from DynamicComponentLoader to ComponentResolver

let component = <Type> viewDecl.component;
dcl.loadIntoLocation(component, elementRef, "content", providers).then(ref => {
this.componentRef = ref;
let createComponent = (factory: ComponentFactory) => {
let parentInjector = this.viewContainerRef.injector;
let childInjector = ReflectiveInjector.resolveAndCreate(rawProviders, parentInjector);
let ref = this.componentRef = this.viewContainerRef.createComponent(factory, undefined, childInjector);

// TODO: wire uiCanExit and uiOnParamsChanged callbacks

// Set resolve data to matching @Input("prop")
let inputs = ng2ComponentInputs(component);
// Supply resolve data to matching @Input('prop') or inputs: ['prop']
let inputs = ng2ComponentInputs(componentType);
let bindings = viewDecl['bindings'] || {};

inputs.map(tuple => ({ prop: tuple.prop, resolve: bindings[tuple.prop] || tuple.resolve }))
.filter(tuple => resolvables[tuple.resolve] !== undefined)
.forEach(tuple => { ref.instance[tuple.prop] = resolvables[tuple.resolve].data });
});
};

this.compResolver.resolveComponent(componentType).then(createComponent);
}
}

0 comments on commit 45c0758

Please sign in to comment.