@@ -11,6 +11,7 @@ import {trace} from "../common/trace";
1111import { Inject } from "angular2/core" ;
1212import { ViewContext , ViewConfig } from "../view/interface" ;
1313import { Ng2ViewDeclaration } from "./interface" ;
14+ import { ng2ComponentInputs } from "./componentUtil" ;
1415
1516/** @hidden */
1617let id = 0 ;
@@ -23,6 +24,12 @@ const getProviders = (injector) => {
2324 return providers ;
2425} ;
2526
27+ // These are provide()d as the string UiView.PARENT_INJECT
28+ export interface ParentUiViewInject {
29+ context : ViewContext ;
30+ fqn : string ;
31+ }
32+
2633/**
2734 * A UI-Router viewport directive, which is filled in by a view (component) on a state.
2835 *
@@ -84,44 +91,39 @@ const getProviders = (injector) => {
8491 // <div style="padding: 1em; border: 1px solid lightgrey;">
8592 //
8693 // <div #content style="color: lightgrey; font-size: smaller;">
87- // <div>ui-view #{{uiViewData.id}} created by '{{ parentContext.name || "(root)" }}' state</div>
88- // <div>name: (absolute) '{{uiViewData.fqn}}' (contextual) '{{uiViewData.name}}@{{parentContext.name}}' </div>
89- // <div>currently filled by: '{{(uiViewData.config && uiViewData.config.viewDecl.$context) || 'empty...'}}'</div>
94+ // <div>ui-view #{{uiViewData? .id}} created by '{{ parentContext? .name || "(root)" }}' state</div>
95+ // <div>name: (absolute) '{{uiViewData? .fqn}}' (contextual) '{{uiViewData? .name}}@{{parentContext? .name}}' </div>
96+ // <div>currently filled by: '{{(uiViewData? .config && uiViewData? .config? .viewDecl? .$context) || 'empty...'}}'</div>
9097 // </div>
9198 //
9299 // </div>`
93100} )
94101export class UiView {
95102 @Input ( ) name : string ;
96- @Input ( ) set 'ui-view' ( val ) { this . name = val ; }
97-
103+ @Input ( 'ui-view' ) set _name ( val ) { this . name = val ; }
98104 componentRef : ComponentRef ;
99105 deregister : Function ;
100106 uiViewData : any = { } ;
101107
102- static INJECT = {
103- fqn : "UiView.parentFQN" ,
104- context : "UiView.parentContext"
105- } ;
108+ static PARENT_INJECT = "UiView.PARENT_INJECT" ;
106109
107110 constructor (
108111 public router : UIRouter ,
109- @Inject ( UiView . INJECT . context ) public parentContext : ViewContext ,
110- @Inject ( UiView . INJECT . fqn ) public parentFqn : string ,
112+ @Inject ( UiView . PARENT_INJECT ) public parent : ParentUiViewInject ,
111113 public dcl : DynamicComponentLoader ,
112114 public elementRef : ElementRef ,
113115 public injector : Injector
114116 ) { }
115117
116118 ngOnInit ( ) {
117- let parentFqn = this . parentFqn ;
119+ let parentFqn = this . parent . fqn ;
118120 let name = this . name || '$default' ;
119121
120122 this . uiViewData = {
121123 id : id ++ ,
122124 name : name ,
123125 fqn : parentFqn ? parentFqn + "." + name : name ,
124- creationContext : this . parentContext ,
126+ creationContext : this . parent . context ,
125127 configUpdated : this . viewConfigUpdated . bind ( this ) ,
126128 config : undefined
127129 } ;
@@ -131,10 +133,11 @@ export class UiView {
131133
132134 disposeLast ( ) {
133135 if ( this . componentRef ) this . componentRef . dispose ( ) ;
136+ this . componentRef = null ;
134137 }
135138
136139 ngOnDestroy ( ) {
137- this . deregister ( ) ;
140+ if ( this . deregister ) this . deregister ( ) ;
138141 this . disposeLast ( ) ;
139142 }
140143
@@ -159,17 +162,26 @@ export class UiView {
159162 let rc = config . node . resolveContext ;
160163 let resolvables = rc . getResolvables ( ) ;
161164 let rawProviders = Object . keys ( resolvables ) . map ( key => provide ( key , { useValue : resolvables [ key ] . data } ) ) ;
162- rawProviders . push ( provide ( UiView . INJECT . context , { useValue : config . viewDecl . $context } ) ) ;
163- rawProviders . push ( provide ( UiView . INJECT . fqn , { useValue : uiViewData . fqn } ) ) ;
165+ rawProviders . push ( provide ( UiView . PARENT_INJECT , { useValue : { context : config . viewDecl . $context , fqn : uiViewData . fqn } } ) ) ;
164166 let providers = Injector . resolve ( rawProviders ) ;
165167
166- let exclusions = [ UiView . INJECT . context , UiView . INJECT . fqn ] ;
168+ let exclusions = [ UiView . PARENT_INJECT ] ;
167169 providers = getProviders ( injector ) . filter ( x => exclusions . indexOf ( x . key . displayName ) === - 1 ) . concat ( providers ) ;
168170
169- // The 'controller' should be a Component class
170- // TODO: pull from 'component' declaration, do not require template.
171171 let component = < Type > viewDecl . component ;
172- dcl . loadIntoLocation ( component , elementRef , "content" , providers ) . then ( ref => this . componentRef = ref ) ;
172+ dcl . loadIntoLocation ( component , elementRef , "content" , providers ) . then ( ref => {
173+ this . componentRef = ref ;
174+
175+ // TODO: wire uiCanExit and uiOnParamsChanged callbacks
176+
177+ // Set resolve data to matching @Input ("prop")
178+ let inputs = ng2ComponentInputs ( component ) ;
179+ let bindings = viewDecl [ 'bindings' ] || { } ;
180+
181+ inputs . map ( tuple => ( { prop : tuple . prop , resolve : bindings [ tuple . prop ] || tuple . resolve } ) )
182+ . filter ( tuple => resolvables [ tuple . resolve ] !== undefined )
183+ . forEach ( tuple => { ref . instance [ tuple . prop ] = resolvables [ tuple . resolve ] . data } ) ;
184+ } ) ;
173185 }
174186}
175187
0 commit comments