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.

For an example of an extended splash screen, refer to the Splash screen sample. One more detail that’s<br />

worth mentioning is that because an extended splash screen is just a page in your app, it can be placed<br />

into the various view states such as snapped view. So, as with every other page in your app, make sure<br />

your extended splash screen handles those states!<br />

Activation Deferrals<br />

As mentioned earlier, once you return from the activated event, Windows assumes that you’ve done<br />

everything you need on startup. By default, then, Windows will remove its splash screen and make your<br />

home page visible. But what if you need to complete one or more async operations before that home<br />

page is really ready, such as completing WinJS.UI.processAll?<br />

This, again, is what the args.setPromise method inside the activated event is for. If you give your<br />

async operation’s promise to setPromise, Windows will wait until that promise is fulfilled before taking<br />

down the splash screen. The templates again use this to keep the system splash screen up until<br />

WinJS.UI.processAll is complete.<br />

As setPromise just waits for a single promise to complete, how do you handle multiple async<br />

operations? You can do this a couple of ways. First, if you need to control the sequencing of those<br />

operations, you can chain them together as we already know how to do—just be sure that the end of<br />

the chain is a promise that becomes the argument to setPromise—don’t call its done method (use then<br />

if needed)! If the sequence isn’t important but you need all of them to complete, you can combine<br />

those promises by using WinJS.Promise.join , passing the result to setPromise. If you need only one of<br />

the operations to complete, you can use WinJS.Promise.any instead—join and any are discussed in the<br />

last section of this chapter.<br />

The other means is to register more than one handler with WinJS.Application.onactivated; each<br />

handler will get its own event args and its own setPromise function, and WinJS will combine those<br />

returned promises together with WinJS.Promise.join.<br />

Now the setPromise method coming from WinJS is actually implemented using a more generic<br />

deferral mechanism from WinRT. The args given to Windows.UI.WebUI.WebUIApplication.-<br />

onactivated (the WinRT event) contains a little method called getDeferral (technically Windows.-<br />

UI.WebUI.ActivatedOperation.getDeferral). This function returns a deferral object that contains a<br />

complete method, and Windows will leave the system splash screen up until you call that method<br />

(although this doesn’t change the fact that users are impatient and your app is still subject to the<br />

15-second limit!). The code looks like this:<br />

//In the activated handler<br />

var activatedDeferral = Windows.UI.WebUI.ActivatedOperation.getDeferral();<br />

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

//After initialization is complete<br />

activatedDeferral.complete();<br />

}<br />

108

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

Saved successfully!

Ooh no, something went wrong!