Skip to content

Commit

Permalink
feat(ng2.uiView): bind resolve data to input[] and @input(), process …
Browse files Browse the repository at this point in the history
…bindings:
  • Loading branch information
christopherthielen committed Apr 5, 2016
1 parent 8a24041 commit f6dae28
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/ng2/componentUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {InputMetadata, ComponentMetadata} from "angular2/core";

export const ng2ComponentInputs = (ng2CompClass) => {
/** Get "@Input('foo') _foo" inputs */
let props = Reflect['getMetadata']('propMetadata', ng2CompClass);
let _props = Object.keys(props || {})
// -> { string, anno[] } tuples
.map(key => ({ key, annoArr: props[key] }))
// -> to { string, anno } tuples
.reduce((acc, tuple) => acc.concat(tuple.annoArr.map(anno => ({ key: tuple.key, anno }))), [])
// Only Inputs
.filter(tuple => tuple.anno instanceof InputMetadata)
// If they have a bindingPropertyName, i.e. "@Input('foo') _foo", then foo, else _foo
.map(tuple => ({ resolve: tuple.anno.bindingPropertyName || tuple.key, prop: tuple.key }));

/** Get "inputs: ['foo']" inputs */
let inputs = Reflect['getMetadata']('annotations', ng2CompClass)
// Find the ComponentMetadata class annotation
.filter(x => x instanceof ComponentMetadata && !!x.inputs)
// Get the .inputs string array
.map(x => x.inputs)
// Flatten
.reduce((acc, arr) => acc.concat(arr), [])
.map(input => ({ resolve: input, prop: input }));

return _props.concat(inputs);
};
17 changes: 14 additions & 3 deletions src/ng2/uiView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {trace} from "../common/trace";
import {Inject} from "angular2/core";
import {ViewContext, ViewConfig} from "../view/interface";
import {Ng2ViewDeclaration} from "./interface";
import {ng2ComponentInputs} from "./componentUtil";

/** @hidden */
let id = 0;
Expand Down Expand Up @@ -168,10 +169,20 @@ export class UiView {
let exclusions = [UiView.PARENT_INJECT];
providers = getProviders(injector).filter(x => exclusions.indexOf(x.key.displayName) === -1).concat(providers);

// The 'controller' should be a Component class
// TODO: pull from 'component' declaration, do not require template.
let component = <Type> viewDecl.component;
dcl.loadIntoLocation(component, elementRef, "content", providers).then(ref => this.componentRef = ref);
dcl.loadIntoLocation(component, elementRef, "content", providers).then(ref => {
this.componentRef = ref;

// TODO: wire uiCanExit and uiOnParamsChanged callbacks

// Set resolve data to matching @Input("prop")
let inputs = ng2ComponentInputs(component);
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 });
});
}
}

0 comments on commit f6dae28

Please sign in to comment.