1
1
import {
2
2
Component ,
3
3
DoCheck ,
4
+ OnDestroy ,
4
5
ElementRef ,
5
6
ViewContainerRef ,
6
7
TemplateRef ,
@@ -14,7 +15,7 @@ import {
14
15
ViewChild ,
15
16
Output ,
16
17
NgZone ,
17
- ChangeDetectionStrategy } from '@angular/core' ;
18
+ ChangeDetectionStrategy } from '@angular/core' ;
18
19
import { isBlank } from '@angular/core/src/facade/lang' ;
19
20
import { isListLikeIterable } from '@angular/core/src/facade/collection' ;
20
21
import { Observable as RxObservable } from 'rxjs'
@@ -23,7 +24,12 @@ import {View} from 'ui/core/view';
23
24
import { NgView } from '../view-util' ;
24
25
import { ObservableArray } from 'data/observable-array' ;
25
26
import { LayoutBase } from 'ui/layouts/layout-base' ;
27
+ < < < << << 2 b36449d70d559d1fda6cdf4d271246b5fe0ecd2
26
28
import { rendererLog , rendererError } from "../trace" ;
29
+ = === ===
30
+ import 'rxjs/add/operator/debounceTime' ;
31
+
32
+ > >>> >>> Manually trigger change detection onItemLoading for current item only
27
33
const NG_VIEW = "_ngViewRef" ;
28
34
29
35
export class ListItemContext {
@@ -52,7 +58,7 @@ export interface SetupItemViewArgs {
52
58
inputs : [ 'items' ] ,
53
59
changeDetection : ChangeDetectionStrategy . OnPush
54
60
} )
55
- export class ListViewComponent {
61
+ export class ListViewComponent implements DoCheck , OnDestroy {
56
62
public get nativeElement ( ) : ListView {
57
63
return this . listView ;
58
64
}
@@ -76,20 +82,24 @@ export class ListViewComponent {
76
82
if ( needDiffer && ! this . _differ && isListLikeIterable ( value ) ) {
77
83
this . _differ = this . _iterableDiffers . find ( this . _items ) . create ( this . _cdr , ( index , item ) => { return item ; } ) ;
78
84
}
85
+
86
+ // this._cdr.detach();
79
87
this . listView . items = this . _items ;
80
88
}
81
89
82
- private timerId : number ;
83
- private doCheckDelay = 5 ;
84
-
85
90
constructor ( private _elementRef : ElementRef ,
86
91
private _iterableDiffers : IterableDiffers ,
87
92
private _cdr : ChangeDetectorRef ,
88
93
private _zone : NgZone ) {
89
94
this . listView = _elementRef . nativeElement ;
95
+
96
+ this . listView . on ( "itemLoading" , this . onItemLoading , this ) ;
97
+ }
98
+
99
+ ngOnDestroy ( ) {
100
+ this . listView . off ( "itemLoading" , this . onItemLoading , this ) ;
90
101
}
91
102
92
- @HostListener ( "itemLoading" , [ '$event' ] )
93
103
public onItemLoading ( args ) {
94
104
if ( ! this . itemTemplate ) {
95
105
return ;
@@ -101,20 +111,23 @@ export class ListViewComponent {
101
111
let viewRef : EmbeddedViewRef < ListItemContext > ;
102
112
103
113
if ( args . view ) {
104
- rendererLog ( "ListView.onItemLoading: " + index + " - Reusing existing view" ) ;
114
+ log ( "ListView.onItemLoading: " + index + " - Reusing existing view" ) ;
115
+
105
116
viewRef = args . view [ NG_VIEW ] ;
106
117
// getting angular view from original element (in cases when ProxyViewContainer is used NativeScript internally wraps it in a StackLayout)
107
118
if ( ! viewRef ) {
108
119
viewRef = ( args . view . _subViews && args . view . _subViews . length > 0 ) ? args . view . _subViews [ 0 ] [ NG_VIEW ] : undefined ;
109
120
}
110
121
}
111
122
else {
112
- rendererLog ( "ListView.onItemLoading: " + index + " - Creating view from template" ) ;
123
+ log ( "ListView.onItemLoading: " + index + " - Creating view from template" ) ;
113
124
viewRef = this . loader . createEmbeddedView ( this . itemTemplate , new ListItemContext ( ) , 0 ) ;
114
125
args . view = getSingleViewFromViewRef ( viewRef ) ;
115
126
args . view [ NG_VIEW ] = viewRef ;
116
127
}
117
128
this . setupViewRef ( viewRef , currentItem , index ) ;
129
+
130
+ this . detectChangesOnChild ( viewRef ) ;
118
131
}
119
132
120
133
public setupViewRef ( viewRef : EmbeddedViewRef < ListItemContext > , data : any , index : number ) : void {
@@ -131,25 +144,42 @@ export class ListViewComponent {
131
144
this . setupItemView . next ( { view : viewRef , data : data , index : index , context : context } ) ;
132
145
}
133
146
147
+ private detectChangesOnChild ( viewRef : EmbeddedViewRef < ListItemContext > ) {
148
+ // Manually detect changes in view ref
149
+ var childCD = < ChangeDetectorRef > ( < any > viewRef ) ;
150
+ var childView = ( < any > viewRef ) . _view ;
151
+
152
+ log ( "------------ detectChanges START ----------" )
153
+ log ( "CangeDetectionState child before mark " + this . logCD ( childView ) ) ;
154
+ childCD . markForCheck ( ) ;
155
+ childCD . detectChanges ( ) ;
156
+ log ( "CangeDetectionState child after detect " + this . logCD ( childView ) ) ;
157
+ log ( "------------ detectChanges END ----------" )
158
+ }
159
+
160
+ private logCD ( cdr : any ) {
161
+ var modes = [ "CheckOnce" , "Checked" , "CheckAlways" , "Detached" , "OnPush" , "Default" ] ;
162
+ var states = [ "Never" , "CheckedBefore" , "Error" ] ;
163
+ return "Mode: " + modes [ parseInt ( cdr . cdMode ) ] + " State: " + states [ parseInt ( cdr . cdState ) ] ;
164
+ }
165
+
134
166
ngDoCheck ( ) {
135
- if ( this . timerId ) {
136
- clearTimeout ( this . timerId ) ;
167
+ if ( this . _differ ) {
168
+ log ( "======> ngDoCheck() DIFFER" )
169
+ var changes = this . _differ . diff ( this . _items ) ;
170
+ if ( changes ) {
171
+ log ( "======> ngDoCheck() REFRESH" )
172
+ // this._cdr.detach();
173
+ this . listView . refresh ( ) ;
174
+ }
137
175
}
138
-
139
- this . _zone . runOutsideAngular ( ( ) => {
140
- this . timerId = setTimeout ( ( ) => {
141
- clearTimeout ( this . timerId ) ;
142
- if ( this . _differ ) {
143
- var changes = this . _differ . diff ( this . _items ) ;
144
- if ( changes ) {
145
- this . listView . refresh ( ) ;
146
- }
147
- }
148
- } , this . doCheckDelay ) ;
149
- } ) ;
150
176
}
151
177
}
152
178
179
+ function log ( msg ) {
180
+ // console.log(msg);
181
+ }
182
+
153
183
function getSingleViewFromViewRef ( viewRef : EmbeddedViewRef < any > ) : View {
154
184
var getSingleViewRecursive = ( nodes : Array < any > , nestLevel : number ) => {
155
185
var actualNodes = nodes . filter ( ( n ) => ! ! n && n . nodeName !== "#text" ) ;
0 commit comments