Skip to content

Commit f6dae28

Browse files
feat(ng2.uiView): bind resolve data to input[] and @input(), process bindings:
1 parent 8a24041 commit f6dae28

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/ng2/componentUtil.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {InputMetadata, ComponentMetadata} from "angular2/core";
2+
3+
export const ng2ComponentInputs = (ng2CompClass) => {
4+
/** Get "@Input('foo') _foo" inputs */
5+
let props = Reflect['getMetadata']('propMetadata', ng2CompClass);
6+
let _props = Object.keys(props || {})
7+
// -> { string, anno[] } tuples
8+
.map(key => ({ key, annoArr: props[key] }))
9+
// -> to { string, anno } tuples
10+
.reduce((acc, tuple) => acc.concat(tuple.annoArr.map(anno => ({ key: tuple.key, anno }))), [])
11+
// Only Inputs
12+
.filter(tuple => tuple.anno instanceof InputMetadata)
13+
// If they have a bindingPropertyName, i.e. "@Input('foo') _foo", then foo, else _foo
14+
.map(tuple => ({ resolve: tuple.anno.bindingPropertyName || tuple.key, prop: tuple.key }));
15+
16+
/** Get "inputs: ['foo']" inputs */
17+
let inputs = Reflect['getMetadata']('annotations', ng2CompClass)
18+
// Find the ComponentMetadata class annotation
19+
.filter(x => x instanceof ComponentMetadata && !!x.inputs)
20+
// Get the .inputs string array
21+
.map(x => x.inputs)
22+
// Flatten
23+
.reduce((acc, arr) => acc.concat(arr), [])
24+
.map(input => ({ resolve: input, prop: input }));
25+
26+
return _props.concat(inputs);
27+
};

src/ng2/uiView.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {trace} from "../common/trace";
1111
import {Inject} from "angular2/core";
1212
import {ViewContext, ViewConfig} from "../view/interface";
1313
import {Ng2ViewDeclaration} from "./interface";
14+
import {ng2ComponentInputs} from "./componentUtil";
1415

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

171-
// The 'controller' should be a Component class
172-
// TODO: pull from 'component' declaration, do not require template.
173172
let component = <Type> viewDecl.component;
174-
dcl.loadIntoLocation(component, elementRef, "content", providers).then(ref => this.componentRef = ref);
173+
dcl.loadIntoLocation(component, elementRef, "content", providers).then(ref => {
174+
this.componentRef = ref;
175+
176+
// TODO: wire uiCanExit and uiOnParamsChanged callbacks
177+
178+
// Set resolve data to matching @Input("prop")
179+
let inputs = ng2ComponentInputs(component);
180+
let bindings = viewDecl['bindings'] || {};
181+
182+
inputs.map(tuple => ({ prop: tuple.prop, resolve: bindings[tuple.prop] || tuple.resolve }))
183+
.filter(tuple => resolvables[tuple.resolve] !== undefined)
184+
.forEach(tuple => { ref.instance[tuple.prop] = resolvables[tuple.resolve].data });
185+
});
175186
}
176187
}
177188

0 commit comments

Comments
 (0)