In WinJS the only way to get the count of items in a ListView object is with the method getCount()
.
But this method is asynchronous.
This make it very difficult to be used in a for loop for example when there is a need to loop through the items of the list.
var listView = document.getElementById("listView").winControl;
listView.itemDataSource.getCount().done(
function (numItems) {
for (var i = 0; i < numItems; i++) {
//do your stuff here
}
});
If I put this in any part of my code I can't return the value I read in the loop from any function because the getCount() return a promise, making my function also return a promise and so on...
So my question is why? Isn't the number of items in a list already known when the method is called?
The ListView's data contract allows for asynchronous data sources, and we include a base class VirtualizedDataSource that you can use for fancy scenarios like that. If you are using a WinJS.Binding.List as your data source that API is in fact synchronous and you should be able to say:
listView.itemDataSource.list.length
However, if you're writing generic code that deals with ListView's and doesn't know what kind of data source it will