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.

In all of this, note again that we don’t need to explicitly reload these variables within the terminated<br />

case because WinJS reloads sessionState automatically. If we managed our state more directly, such as<br />

storing some variables in roaming settings within the checkpoint event, we would reload and apply<br />

those values at this time.<br />

Note Using ms-appdata:/// and getFileFromPathAsync works because the file exists in a location<br />

that we can access programmatically by default. It also works for libraries for which we declare a<br />

capability in the manifest. If, however, we obtained a StorageFile from the file picker, we’d need to<br />

save that in the Windows.Storage.AccessCache to preserve access permissions across sessions.<br />

Data from Services and WinJS.xhr<br />

Though we’ve seen examples of using data from an app’s package (via URIs or Windows.ApplicationModel.Package.current.installedLocation)<br />

as well as in appdata, it’s very likely that your app<br />

will incorporate data from a web service and possibly send data to services as well. For this, the most<br />

common method is to employ XmlHttpRequest. You can use this in its raw (async) form, if you like, or<br />

you can save yourself a whole lot of trouble by using the WinJS.xhr function, which conveniently wraps<br />

the whole business inside a promise.<br />

Making the call is quite easy, as demonstrated in the SimpleXhr example for this chapter. Here we<br />

use WinJS.xhr to retrieve the RSS feed from the Windows 8 developer blog:<br />

WinJS.xhr({ url: "http://blogs.msdn.com/b/windowsappdev/rss.aspx" })<br />

.done(processPosts, processError, showProgress);<br />

That is, give WinJS.xhr a URI and it gives back a promise that delivers its results to your completed<br />

handler (in this case processPosts) and will even call a progress handler if provided. With the former,<br />

the result contains a responseXML property, which is a DomParser object. With the latter, the event object<br />

contains the current XML in its response property, which we can easily use to display a download count:<br />

function showProgress(e) {<br />

var bytes = Math.floor(e.response.length / 1024);<br />

document.getElementById("status").innerText = "Downloaded " + bytes + " KB";<br />

}<br />

The rest of the app just chews on the response text looking for item elements and displaying the<br />

title, pubDate, and link fields. With a little styling (see default.css), and utilizing the WinJS typography<br />

style classes of win-type-x-large (for title), win-type-medium (for pubDate), and win-type-small (for<br />

link), we get a quick app that looks like Figure 3-9. You can look at the code to see the details. 22<br />

22 WinRT has a specific API for dealing with RSS feeds in Windows.Web.Syndication, which we’ll see in Chapter 14. You can<br />

use this if you want a more structured means of dealing with such data sources. As it is, JavaScript has intrinsic APIs to<br />

work with XML, so it’s really your choice. In a case like this, the syndication API along with Windows.Web.AtomPub and<br />

Windows.Data.Xml are very much needed by Windows 8 apps written in other languages that don’t have the same built-in<br />

features as JavaScript.<br />

116

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

Saved successfully!

Ooh no, something went wrong!