@@ -19,13 +19,12 @@ import {
19
19
Host ,
20
20
ChangeDetectionStrategy
21
21
} from "@angular/core" ;
22
- import { isBlank } from "../lang-facade" ;
23
22
import { isListLikeIterable } from "../collection-facade" ;
24
- import { ListView } from "tns-core-modules/ui/list-view" ;
23
+ import { ListView , ItemEventData } from "tns-core-modules/ui/list-view" ;
25
24
import { View , KeyedTemplate } from "tns-core-modules/ui/core/view" ;
26
25
import { ObservableArray } from "tns-core-modules/data/observable-array" ;
27
26
import { LayoutBase } from "tns-core-modules/ui/layouts/layout-base" ;
28
- import { listViewLog } from "../trace" ;
27
+ import { listViewLog , listViewError } from "../trace" ;
29
28
30
29
const NG_VIEW = "_ngViewRef" ;
31
30
@@ -147,27 +146,31 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
147
146
this . _templateMap . set ( key , keyedTemplate ) ;
148
147
}
149
148
150
- public onItemLoading ( args ) {
149
+ public onItemLoading ( args : ItemEventData ) {
151
150
if ( ! args . view && ! this . itemTemplate ) {
152
151
return ;
153
152
}
154
153
155
- let index = args . index ;
156
- let items = args . object . items ;
157
- let currentItem = typeof ( items . getItem ) === "function" ?
158
- items . getItem ( index ) : items [ index ] ;
154
+ const index = args . index ;
155
+ const items = ( < any > args . object ) . items ;
156
+ const currentItem = typeof items . getItem === "function" ? items . getItem ( index ) : items [ index ] ;
159
157
let viewRef : EmbeddedViewRef < ListItemContext > ;
160
158
161
- if ( args . view && args . view [ NG_VIEW ] ) {
159
+ if ( args . view ) {
162
160
listViewLog ( "onItemLoading: " + index + " - Reusing existing view" ) ;
163
161
viewRef = args . view [ NG_VIEW ] ;
164
- // getting angular view from original element (in cases when ProxyViewContainer
162
+ // Getting angular view from original element (in cases when ProxyViewContainer
165
163
// is used NativeScript internally wraps it in a StackLayout)
164
+ if ( ! viewRef && args . view instanceof LayoutBase && args . view . getChildrenCount ( ) > 0 ) {
165
+ viewRef = args . view . getChildAt ( 0 ) [ NG_VIEW ] ;
166
+ }
167
+
166
168
if ( ! viewRef ) {
167
- viewRef = ( args . view . _subViews && args . view . _subViews . length > 0 ) ?
168
- args . view . _subViews [ 0 ] [ NG_VIEW ] : undefined ;
169
+ listViewError ( "ViewReference not found for item " + index + ". View recycling is not working" ) ;
169
170
}
170
- } else {
171
+ } ;
172
+
173
+ if ( ! viewRef ) {
171
174
listViewLog ( "onItemLoading: " + index + " - Creating view from template" ) ;
172
175
viewRef = this . loader . createEmbeddedView ( this . itemTemplate , new ListItemContext ( ) , 0 ) ;
173
176
args . view = getItemViewRoot ( viewRef ) ;
@@ -180,9 +183,6 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
180
183
}
181
184
182
185
public setupViewRef ( viewRef : EmbeddedViewRef < ListItemContext > , data : any , index : number ) : void {
183
- if ( isBlank ( viewRef ) ) {
184
- return ;
185
- }
186
186
const context = viewRef . context ;
187
187
context . $implicit = data ;
188
188
context . item = data ;
@@ -194,13 +194,9 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
194
194
}
195
195
196
196
private detectChangesOnChild ( viewRef : EmbeddedViewRef < ListItemContext > , index : number ) {
197
- // Manually detect changes in child view ref
198
- // TODO: Is there a better way of getting viewRef"s change detector
199
- const childChangeDetector = < ChangeDetectorRef > ( < any > viewRef ) ;
200
-
201
197
listViewLog ( "Manually detect changes in child: " + index ) ;
202
- childChangeDetector . markForCheck ( ) ;
203
- childChangeDetector . detectChanges ( ) ;
198
+ viewRef . markForCheck ( ) ;
199
+ viewRef . detectChanges ( ) ;
204
200
}
205
201
206
202
ngDoCheck ( ) {
0 commit comments