import { ObservableArray } from "tns-core-modules/data/observable-array" ; import { Observable } from "tns-core-modules/data/observable" ; import { ListViewEventData } from "nativescript-ui-listview" ; export class ViewModel extends Observable { constructor ( ) { super ( ) ; this . initDataItems ( ) ; } get dataItems ( ) : ObservableArray < DataItem > { return this . get ( "_dataItems" ) ; } set dataItems ( value ) { this . set ( "_dataItems" , value ) ; } public itemSelected ( args ) { const item = this . dataItems . getItem ( args . index ) ; console . log ( "Selected item: " + item . name ) ; item . selected = true ; } public itemSelecting ( args ) { const item = this . dataItems . getItem ( args . index ) ; console . log ( "Selecting item: " + item . name ) ; } public itemDeselecting ( args ) { const item = this . dataItems . getItem ( args . index ) ; console . log ( "Deselecting item: " + item . name ) ; } public itemDeselected ( args ) { const item = this . dataItems . getItem ( args . index ) ; console . log ( "Deselected item: " + item . name ) ; item . selected = false ; } private initDataItems ( ) { this . dataItems = new ObservableArray < DataItem > ( ) ; for ( let i = 0 ; i < 25 ; i ++ ) { this . dataItems . push ( new DataItem ( i , "Item " + i , "This is item description." ) ) ; } } } export class DataItem extends Observable { public get selected ( ) { return this . get ( '_selected' ) ; } public set selected ( value ) { this . set ( '_selected' , value ) ; } public get name ( ) { return this . get ( '_itemName' ) ; } public set name ( value ) { this . set ( '_itemName' , value ) ; } public get description ( ) { return this . get ( '_itemDescription' ) ; } public set description ( value ) { this . set ( '_itemDescription' , value ) ; } public get id ( ) { return this . get ( '_id' ) ; } public set id ( value ) { this . set ( '_id' , value ) ; } constructor ( id , name , description ) { super ( ) ; this . id = id ; this . name = name ; this . description = description ; this . selected = false ; } }