11import { Component , Input , ChangeDetectionStrategy } from '@angular/core' ;
2- import { Observable as RxObservable } from 'rxjs/Observable' ;
3-
4- export class DataItem {
5- constructor ( public id : number , public name : string ) { }
6- }
2+ import * as Rx from 'rxjs/Observable' ;
3+ import { combineLatestStatic } from 'rxjs/operator/combineLatest' ;
4+ import { BehaviorSubject } from "rxjs/BehaviorSubject" ;
5+ import { DataItem , DataService } from "./data.service"
76
87@Component ( {
98 selector : 'list-test-async' ,
109 styleUrls : [ 'examples/list/list-test-async.css' ] ,
10+ providers : [ DataService ] ,
11+ changeDetection : ChangeDetectionStrategy . OnPush ,
1112 template : `
12- <GridLayout>
13- <ListView [items]="myItems | async" (itemTap)="onItemTap($event)">
13+ <GridLayout rows="auto * auto" columns="* *">
14+ <Label text="ListView" class="list-title"></Label>
15+ <Label text="*ngFor" class="list-title" col="1"></Label>
16+
17+ <ListView row="1" [items]="service.items$ | async" (itemTap)="onItemTap($event)" margin="10">
1418 <template let-item="item" let-i="index" let-odd="odd" let-even="even">
1519 <StackLayout [class.odd]="odd" [class.even]="even">
16- <Label class="test" [text]='"index: " + item.name'></Label>
20+ <Label [text]='"index: " + item.name'></Label>
1721 </StackLayout>
1822 </template>
1923 </ListView>
24+
25+ <StackLayout row="1" col="1" margin="10">
26+ <StackLayout *ngFor="let item of (service.items$ | async); let odd = odd; let even = even"
27+ [class.odd]="odd" [class.even]="even" marginBottom="1">
28+ <Label [text]='"index: " + item.name'></Label>
29+ </StackLayout>
30+ </StackLayout>
31+
32+ <button row="2" colSpan="2" (tap)="toggleAsyncUpdates()" [text]="isUpdating ? 'stop updates' : 'start updates'"></button>
2033 </GridLayout>
21- ` ,
22- changeDetection : ChangeDetectionStrategy . OnPush
34+ `
2335} )
2436export class ListTestAsync {
25- public myItems : RxObservable < Array < DataItem > > ;
37+ public isUpdating : boolean = false ;
38+ constructor ( private service : DataService ) {
39+ }
40+
41+ public onItemTap ( args ) {
42+ console . log ( "--> ItemTapped: " + args . index ) ;
43+ }
2644
27- constructor ( ) {
28- var items = [ ] ;
29- for ( var i = 0 ; i < 3 ; i ++ ) {
30- items . push ( new DataItem ( i , "data item " + i ) ) ;
45+ public toggleAsyncUpdates ( ) {
46+ if ( this . isUpdating ) {
47+ this . service . stopAsyncUpdates ( ) ;
48+ } else {
49+ this . service . startAsyncUpdates ( ) ;
3150 }
32-
33- var subscr ;
34- this . myItems = RxObservable . create ( subscriber => {
35- subscr = subscriber ;
36- subscriber . next ( items ) ;
37- return function ( ) {
38- console . log ( "Unsubscribe called!!!" ) ;
39- }
40- } ) ;
4151
42- let counter = 2 ;
43- let intervalId = setInterval ( ( ) => {
44- counter ++ ;
45- console . log ( "Adding " + counter + "-th item" ) ;
46- items . push ( new DataItem ( counter , "data item " + counter ) ) ;
47- subscr . next ( items ) ;
48- } , 1000 ) ;
49-
50- setTimeout ( ( ) => {
51- clearInterval ( intervalId ) ;
52- } , 15000 ) ;
52+ this . isUpdating = ! this . isUpdating ;
53+ }
54+ }
55+
56+ @Component ( {
57+ selector : 'list-test-async-filter' ,
58+ styleUrls : [ 'examples/list/list-test-async.css' ] ,
59+ providers : [ DataService ] ,
60+ // changeDetection: ChangeDetectionStrategy.OnPush,
61+ template : `
62+ <GridLayout rows="auto * auto" columns="* *">
63+ <Label text="ListView" class="list-title"></Label>
64+ <Label text="*ngFor" class="list-title" col="1"></Label>
65+
66+ <ListView row="1" [items]="filteredItems$ | async" (itemTap)="onItemTap($event)" margin="10">
67+ <template let-item="item" let-i="index" let-odd="odd" let-even="even">
68+ <StackLayout [class.odd]="odd" [class.even]="even">
69+ <Label [text]='"index: " + item.name'></Label>
70+ </StackLayout>
71+ </template>
72+ </ListView>
73+
74+ <StackLayout row="1" col="1" margin="10">
75+ <StackLayout *ngFor="let item of (filteredItems$ | async); let odd = odd; let even = even"
76+ [class.odd]="odd" [class.even]="even" marginBottom="1">
77+ <Label [text]='"index: " + item.name'></Label>
78+ </StackLayout>
79+ </StackLayout>
80+
81+ <StackLayout row="2" colSpan="2" orientation="horizontal">
82+ <button (tap)="toggleAsyncUpdates()" [text]="isUpdating ? 'stop updates' : 'start updates'"></button>
83+ <button (tap)="toogleFilter()" [text]="(filter$ | async) ? 'show all' : 'show even'"></button>
84+ </StackLayout>
85+ </GridLayout>
86+ `
87+ } )
88+ export class ListTestFilterAsync {
89+ public isUpdating : boolean = false ;
90+ public filteredItems$ : Rx . Observable < Array < DataItem > > ;
91+ private filter$ = new BehaviorSubject ( false ) ;
92+
93+ constructor ( private service : DataService ) {
94+ // Create filteredItems$ by combining the service.items$ and filter$
95+ this . filteredItems$ = combineLatestStatic ( this . service . items$ , this . filter$ , ( data , filter ) => {
96+ return filter ? data . filter ( v => v . id % 2 === 0 ) : data ;
97+ } ) ;
5398 }
5499
55100 public onItemTap ( args ) {
56- console . log ( "------------------------ ItemTapped: " + args . index ) ;
101+ console . log ( "--> ItemTapped: " + args . index ) ;
57102 }
58- }
103+
104+ public toggleAsyncUpdates ( ) {
105+ if ( this . isUpdating ) {
106+ this . service . stopAsyncUpdates ( ) ;
107+ } else {
108+ this . service . startAsyncUpdates ( ) ;
109+ }
110+
111+ this . isUpdating = ! this . isUpdating ;
112+ }
113+
114+ public toogleFilter ( ) {
115+ this . filter$ . next ( ! this . filter$ . value ) ;
116+ }
117+ }
0 commit comments