1
1
/** @module ng2_directives */ /** */
2
- import { Component , ElementRef , DynamicComponentLoader } from 'angular2/core' ;
3
- import { Injector } from "angular2/core" ;
2
+ import {
3
+ Component , ComponentResolver , ComponentFactory ,
4
+ ViewContainerRef , ReflectiveInjector
5
+ } from 'angular2/core' ;
4
6
import { provide } from "angular2/core" ;
5
7
import { Input } from "angular2/core" ;
6
8
import { ComponentRef } from "angular2/core" ;
@@ -17,14 +19,6 @@ import {Ng2ViewConfig} from "./viewsBuilder";
17
19
/** @hidden */
18
20
let id = 0 ;
19
21
20
- const getProviders = ( injector ) => {
21
- let providers = [ ] , parentInj = injector . parent ;
22
- for ( let i = 0 ; i < parentInj . _proto . numberOfProviders ; i ++ ) {
23
- providers . push ( parentInj . _proto . getProviderAtIndex ( i ) ) ;
24
- }
25
- return providers ;
26
- } ;
27
-
28
22
// These are provide()d as the string UiView.PARENT_INJECT
29
23
export interface ParentUiViewInject {
30
24
context : ViewContext ;
@@ -81,13 +75,13 @@ export interface ParentUiViewInject {
81
75
*/
82
76
@Component ( {
83
77
selector : 'ui-view, [ui-view]' ,
84
- styles : [ `
85
- .done-true {
86
- text-decoration: line-through;
87
- color: grey ;
88
- }`
89
- ] ,
90
- template : `<div #content></div>` ,
78
+ template : ''
79
+ // styles: [`
80
+ // .done-true {
81
+ // text-decoration: line-through ;
82
+ // color: grey;
83
+ // }`
84
+ // ] ,
91
85
// template: `
92
86
// <div style="padding: 1em; border: 1px solid lightgrey;">
93
87
//
@@ -100,7 +94,7 @@ export interface ParentUiViewInject {
100
94
// </div>`
101
95
} )
102
96
export class UiView {
103
- @Input ( ) name : string ;
97
+ @Input ( 'name' ) name : string ;
104
98
@Input ( 'ui-view' ) set _name ( val ) { this . name = val ; }
105
99
componentRef : ComponentRef ;
106
100
deregister : Function ;
@@ -111,9 +105,8 @@ export class UiView {
111
105
constructor (
112
106
public router : UIRouter ,
113
107
@Inject ( UiView . PARENT_INJECT ) public parent : ParentUiViewInject ,
114
- public dcl : DynamicComponentLoader ,
115
- public elementRef : ElementRef ,
116
- public injector : Injector
108
+ public compResolver : ComponentResolver ,
109
+ public viewContainerRef : ViewContainerRef
117
110
) { }
118
111
119
112
ngOnInit ( ) {
@@ -134,7 +127,7 @@ export class UiView {
134
127
}
135
128
136
129
disposeLast ( ) {
137
- if ( this . componentRef ) this . componentRef . dispose ( ) ;
130
+ if ( this . componentRef ) this . componentRef . destroy ( ) ;
138
131
this . componentRef = null ;
139
132
}
140
133
@@ -147,7 +140,7 @@ export class UiView {
147
140
if ( ! config ) return this . disposeLast ( ) ;
148
141
if ( ! ( config instanceof Ng2ViewConfig ) ) return ;
149
142
150
- let { uiViewData, injector , dcl , elementRef } = this ;
143
+ let uiViewData = this . uiViewData ;
151
144
let viewDecl = < Ng2ViewDeclaration > config . viewDecl ;
152
145
153
146
// The "new" viewconfig is already applied, so exit early
@@ -159,30 +152,32 @@ export class UiView {
159
152
// The config may be undefined if there is nothing state currently targeting this UiView.
160
153
if ( ! config ) return ;
161
154
162
- // Do some magic
155
+ // Map resolves to "useValue providers"
163
156
let rc = config . node . resolveContext ;
164
157
let resolvables = rc . getResolvables ( ) ;
165
158
let rawProviders = Object . keys ( resolvables ) . map ( key => provide ( key , { useValue : resolvables [ key ] . data } ) ) ;
166
159
rawProviders . push ( provide ( UiView . PARENT_INJECT , { useValue : { context : config . viewDecl . $context , fqn : uiViewData . fqn } } ) ) ;
167
- let providers = Injector . resolve ( rawProviders ) ;
168
160
169
- let exclusions = [ UiView . PARENT_INJECT ] ;
170
- providers = getProviders ( injector ) . filter ( x => exclusions . indexOf ( x . key . displayName ) === - 1 ) . concat ( providers ) ;
161
+ // Get the component class from the view declaration. TODO: allow promises?
162
+ let componentType = < Type > viewDecl . component ;
171
163
172
- let component = < Type > viewDecl . component ;
173
- dcl . loadIntoLocation ( component , elementRef , "content" , providers ) . then ( ref => {
174
- this . componentRef = ref ;
164
+ let createComponent = ( factory : ComponentFactory ) => {
165
+ let parentInjector = this . viewContainerRef . injector ;
166
+ let childInjector = ReflectiveInjector . resolveAndCreate ( rawProviders , parentInjector ) ;
167
+ let ref = this . componentRef = this . viewContainerRef . createComponent ( factory , undefined , childInjector ) ;
175
168
176
169
// TODO: wire uiCanExit and uiOnParamsChanged callbacks
177
170
178
- // Set resolve data to matching @Input (" prop")
179
- let inputs = ng2ComponentInputs ( component ) ;
171
+ // Supply resolve data to matching @Input (' prop') or inputs: ['prop']
172
+ let inputs = ng2ComponentInputs ( componentType ) ;
180
173
let bindings = viewDecl [ 'bindings' ] || { } ;
181
174
182
175
inputs . map ( tuple => ( { prop : tuple . prop , resolve : bindings [ tuple . prop ] || tuple . resolve } ) )
183
176
. filter ( tuple => resolvables [ tuple . resolve ] !== undefined )
184
177
. forEach ( tuple => { ref . instance [ tuple . prop ] = resolvables [ tuple . resolve ] . data } ) ;
185
- } ) ;
178
+ } ;
179
+
180
+ this . compResolver . resolveComponent ( componentType ) . then ( createComponent ) ;
186
181
}
187
182
}
188
183
0 commit comments