Skip to content

Commit 2afda3e

Browse files
feat(resolve): Map resolves to component props using instance.__inputs added by @ResolveData() decorator
This feature will be removed in a future version, when Angular exposes component metadata (`@Input()`) Closes #36
1 parent ffe85cb commit 2afda3e

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

src/directives/uiView.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
/** @ng2api @module directives */ /** */
1+
/** @ng2api @module directives */
2+
/** */
23
import {
3-
Component, ComponentFactoryResolver, ViewContainerRef, Input, ComponentRef, Type,
4-
ReflectiveInjector, ViewChild, Injector, Inject
4+
Component, ComponentFactoryResolver, ViewContainerRef, Input, ComponentRef, Type, ReflectiveInjector, ViewChild,
5+
Injector, Inject
56
} from '@angular/core';
6-
import {ReflectorReader, reflector} from '../private_import_core';
7-
7+
import { reflector } from '../private_import_core';
88
import {
9-
UIRouter, isFunction, Transition, parse, HookResult, TransitionHookFn, State, prop, StateDeclaration, inArray
10-
} from "ui-router-core";
11-
import {trace} from "ui-router-core";
12-
import {ViewContext, ViewConfig, ActiveUIView} from "ui-router-core";
13-
import {Ng2ViewConfig} from "../statebuilders/views";
14-
import {ResolveContext, NATIVE_INJECTOR_TOKEN} from "ui-router-core";
15-
import {flattenR} from "ui-router-core";
16-
import {MergeInjector} from "../mergeInjector";
17-
import { Subscription } from 'rxjs/Subscription';
9+
UIRouter, isFunction, Transition, parse, TransitionHookFn, StateDeclaration, inArray, trace, ViewContext, ViewConfig,
10+
ActiveUIView, ResolveContext, NATIVE_INJECTOR_TOKEN, flattenR
11+
} from 'ui-router-core';
12+
import { Ng2ViewConfig } from '../statebuilders/views';
13+
import { MergeInjector } from '../mergeInjector';
1814

1915
/** @hidden */
2016
let id = 0;
@@ -39,7 +35,7 @@ interface InputMapping {
3935
*
4036
* @internalapi
4137
*/
42-
const ng2ComponentInputs = (ng2CompClass: Type<any>) => {
38+
const ng2ComponentInputs = (ng2CompClass: Type<any>, component: any) => {
4339
/** Get "@Input('foo') _foo" inputs */
4440
let props = reflector.propMetadata(ng2CompClass);
4541
let _props = Object.keys(props || {})
@@ -61,7 +57,12 @@ const ng2ComponentInputs = (ng2CompClass: Type<any>) => {
6157
.reduce(flattenR, [])
6258
.map(input => ({ token: input, prop: input }));
6359

64-
return _props.concat(inputs) as InputMapping[];
60+
/** Get @ResolveData('foo') _foo" inputs */
61+
let __inputs = component.__inputs || {};
62+
let resolves = Object.keys(__inputs)
63+
.map(key => ({ token: key, prop: __inputs[key] }));
64+
65+
return _props.concat(inputs).concat(resolves) as InputMapping[];
6566
};
6667

6768
/**
@@ -282,15 +283,16 @@ export class UIView {
282283
* to the resolve data.
283284
*/
284285
applyInputBindings(ref: ComponentRef<any>, context: ResolveContext, componentClass) {
285-
let bindings = this.uiViewData.config.viewDecl['bindings'] || {};
286-
let explicitBoundProps = Object.keys(bindings);
286+
const component = ref.instance;
287+
const bindings = this.uiViewData.config.viewDecl['bindings'] || {};
288+
const explicitBoundProps = Object.keys(bindings);
287289

288290
// Supply resolve data to matching @Input('prop') or inputs: ['prop']
289-
let explicitInputTuples = explicitBoundProps
291+
const explicitInputTuples = explicitBoundProps
290292
.reduce((acc, key) => acc.concat([{ prop: key, token: bindings[key] }]), []);
291-
let implicitInputTuples = ng2ComponentInputs(componentClass)
292-
.filter(tuple => !inArray(explicitBoundProps, tuple.prop));
293293

294+
const implicitInputTuples = ng2ComponentInputs(componentClass, component)
295+
.filter(tuple => !inArray(explicitBoundProps, tuple.prop));
294296

295297
const addResolvable = (tuple: InputMapping) => ({
296298
prop: tuple.prop,
@@ -300,7 +302,7 @@ export class UIView {
300302
explicitInputTuples.concat(implicitInputTuples)
301303
.map(addResolvable)
302304
.filter(tuple => tuple.resolvable && tuple.resolvable.resolved)
303-
.forEach(tuple => { ref.instance[tuple.prop] = tuple.resolvable.data });
305+
.forEach(tuple => { component[tuple.prop] = tuple.resolvable.data });
304306

305307
// Initiate change detection for the newly created component
306308
ref.changeDetectorRef.detectChanges();

src/statebuilders/views.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @module ng2 */ /** */
2-
import { State, StateObject } from "ui-router-core";
2+
import {StateObject} from "ui-router-core";
33
import {PathNode} from "ui-router-core";
44
import {pick, forEach} from "ui-router-core";
55
import {ViewConfig} from "ui-router-core";

0 commit comments

Comments
 (0)