23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Data Sources<br />

In all the examples we’ve seen thus far, we’ve been using an in-memory data source built on WinJS.-<br />

Binding.List. Clearly, there are other types of data sources and it certainly doesn’t make sense to load<br />

everything into memory first. How, then, do we work with such sources?<br />

WinJS provides some help in this area. First is the WinJS.UI.StorageDataSource object that works<br />

with files in the file system, as the next section demonstrates with a FlipView and the Pictures Library.<br />

The other is WinJS.UI.VirtualizedDataSource, which is meant for you to use as a base class for a<br />

custom data source of your own, an advanced scenario that we’ll touch on only briefly.<br />

A FlipView Using the Pictures Library<br />

For everything we’ve seen in the FlipView sample already, it really begs for the ability to do something<br />

completely obvious: flip through pictures files in a folder. Using what we’ve learned so far, how would<br />

we implement something like that? We already have an item template containing an img tag, so<br />

perhaps we just need some URIs for those files. Perhaps we could make an array of these using an API<br />

like Windows.Storage.KnownFolders.picturesLibrary.getFilesAsync (declaring the Pictures Library<br />

capability in the manifest, of course!). This would give us a bunch of StorageFile objects for which we<br />

could call URL.createObjectURL. We could store those URIs in an array and then wrap it up with<br />

WinJS.Binding.List:<br />

var myFlipView = document.getElementById("pictures_FlipView").winControl;<br />

Windows.Storage.KnownFolders.picturesLibrary.getFilesAsync()<br />

.done(function (files) {<br />

var pixURLs = [];<br />

files.forEach(function (item) {<br />

var url = URL.createObjectURL(item, {oneTimeOnly: true });<br />

pixURLs.push({type: "item", title: item.name, picture: url });<br />

});<br />

var pixList = new WinJS.Binding.List(pixURLs);<br />

myFlipView.itemDataSource = pixList.dataSource;<br />

});<br />

Although this approach works, it can consume quite a bit of memory with a larger number of<br />

high-resolution pictures because each picture has to be fully loaded to be displayed in the FlipView. This<br />

might be just fine for your scenario but in other cases would consume more resources than necessary. It<br />

also has the drawback that the images are just stretched or compressed to fit into the FlipView without<br />

any concern for aspect ratio, and this doesn’t produce the best results.<br />

A better approach is to use the WinJS.UI.StorageDataSource that again works directly with the file<br />

system instead of an in-memory array. I’ve implemented this as a Scenario 8 in the modified FlipView<br />

202

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!