23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

sample code in this chapter’s companion content. (Another example can be found in the<br />

StorageDataSource and GetVirtualizedFilesVector sample.) Here we can use a shortcut to get a data<br />

source for the Pictures library:<br />

myFlipView.itemDataSource = new WinJS.UI.StorageDataSource("Pictures");<br />

"Pictures" is a shortcut because the first argument to StorageDataSource is actually something<br />

called a file query that comes from the Windows.Storage.Search API, a subject we’ll see in more detail<br />

in Chapter 8, “State, Settings, Files, and Documents.” These queries, which feed into the powerful<br />

Windows.Storage.StorageFolder.createFileQueryWithOptions function, are ways to enumerate<br />

files in a folder along with metadata like album covers, track details, and thumbnails that are cropped to<br />

maintain the aspect ratio. Shortcuts like "Pictures" (also "Music", "Documents", and "Videos" that all<br />

require the associated capability in the manifest) just create typical queries for those document libraries.<br />

The caveat with StorageDataSource is that it’s doesn’t directly support one-way binding, so you’ll get<br />

an exception if you try to refer to item properties directly in a template. To work around this, you have<br />

to explicitly use WinJS.Binding.oneTime as the initializer function for each property:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

In the case of the img.src property, the file query gives us items of type Windows.Storage.-<br />

BulkAccess.FileInformation (the source variable in the code below), which contains a thumbnail<br />

image, not a URI. To convert that image data into a URI, we need to use our own binding initializer:<br />

WinJS.Namespace.define("InitFunctions", {<br />

thumbURL: WinJS.Binding.initializer(function (source, sourceProp, dest, destProp) {<br />

if (source.thumbnail) {<br />

dest.src = URL.createObjectURL(source.thumbnail, { oneTimeOnly: true });<br />

}<br />

})<br />

});<br />

In this initializer, the src : thumbnail part of data-win-bind is actually ignored because we’re just<br />

setting the image’s src property directly to source.thumbnail. This is just a form of one-way binding.<br />

Note that thumbnails aren’t always immediately available in the FileInformation object, which is<br />

why we have to verify that we actually have one before creating a URI for it. This means that quickly<br />

flipping through the images might show some blanks. To solve this particular issue, we can listen for the<br />

FileInformation.onthumbnailupdated event and update the item at that time. The best way to<br />

accomplish this is to use the StorageDataSource.loadThumbnail helper, which makes sure to call<br />

removeEventListener for this WinRT event. (See “WinRT Events and removeEventListener” in Chapter 3.)<br />

203

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

Saved successfully!

Ooh no, something went wrong!