Virtual Array module

import * as virtualArrayModule from "tns-core-modules/data/virtual-array";

Handle "itemsLoading" event to load items on demand using load() method.

Use "length" property set via VirtualArray constructor to specify total number of items, "loadSize" to specify number of items to be requested in a single request, "itemsLoading" event to handle items request and "load()" method to copy items into the array. All already loaded items are cached in -memory and when "getItem()" method is called the array will raise "itemsLoading" event for still not loaded items.

var array = new virtualArrayModule.VirtualArray<number>(100);
array.loadSize = 15;


array.on(virtualArrayModule.VirtualArray.itemsLoadingEvent, (args: virtualArrayModule.ItemsLoading) => {
    // Argument (args) is ItemsLoading.
    // args.index is start index of the page where the requested index is located.
    // args.count number of requested items.
    //
    // Note: Virtual array will divide total number of items to pages using "loadSize" property value. When you request an 
    // item at specific index the array will raise "itemsLoading" event with "ItemsLoading" argument index set to the first index of the requested page
    // and count set to number of items in this page. 
    //
    // Important: If you have already loaded items in the requested page the array will raise multiple times "itemsLoading" event to request 
    // all ranges of still not loaded items in this page. 

    var itemsToLoad = new Array<number>();
    for (var i = 0; i < args.count; i++) {
        itemsToLoad.push(i + args.index);
    }

    array.load(args.index, itemsToLoad);
});

Handle "change" event when you load items using load() method.

var array = new virtualArrayModule.VirtualArray<number>(100);
array.loadSize = 15;


array.on(virtualArrayModule.VirtualArray.changeEvent, (args: virtualArrayModule.ChangedData<number>) => {
    // Argument (args) is ChangedData<T>.
    // args.eventName is "change".
    // args.action is "update".
    // args.removed.length and result.addedCount are equal to number of loaded items with load() method.

});

var itemsToLoad = [0, 1, 2];

array.load(index, itemsToLoad);

Handle "change" event when you increase "length" property.

var array = new virtualArrayModule.VirtualArray<number>(100);
array.loadSize = 15;


array.on(virtualArrayModule.VirtualArray.changeEvent, (args: virtualArrayModule.ChangedData<number>) => {
    // Argument (args) is ChangedData<T>.
    // args.eventName is "change".
    // args.action is "add".
    // args.removed.length is 0, result.addedCount is equal to the delta between new and old "length" property values.

});

array.length += array.loadSize;

Stay connected with NativeScript

(expect a newsletter every 4-8 weeks)

NativeScript
NativeScript is licensed under the Apache 2.0 license .
© Progress Software Corporation. All Rights Reserved.