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.

• settings Fired in response to Windows.UI.ApplicationSettings.SettingsPane.-<br />

oncommandsrequested. (See Chapter 8.)<br />

With most of these events (except error and settings), the args you receive contains a method<br />

called setPromise. If you need to perform an async operation (like an XmlHttpRequest) within an event<br />

handler, you can obtain the promise for that work and hand it off to setPromise instead of calling its<br />

then or done yourself. WinJS will then not process the next event in the queue until that promise is<br />

fulfilled. Now to be honest, there’s no actual difference between this and just calling done on the<br />

promise yourself within the loaded, ready, and unload events. It does make a difference with activated<br />

and checkpoint (specifically the suspending case) because Windows will otherwise assume that you’ve<br />

done everything you need as soon as you return from the handler; more on this in the “Activation<br />

Deferrals” section. So, in general, if you have async work within these events handlers, it’s a good habit<br />

to use setPromise. Because WinJS.UI.processAll is itself an async operation, the templates wrap it with<br />

setPromise so that the splash screen isn’t removed until WinJS controls have been fully instantiated.<br />

I think you’ll generally find WinJS.Application to be a useful tool in your apps, and it also provides a<br />

few more features as documented on the WinJS.Application page. For example, it provides local, temp,<br />

roaming, and sessionState properties, which are helpful for managing state as we’ll see later on in this<br />

chapter and in Chapter 8.<br />

The other bits are the queueEvent and stop methods. The queueEvent method drops an event into<br />

the queue that will get dispatched, after any existing queue is clear, to whatever listeners you’ve set up<br />

on the WinJS.Application object. Events are simply identified with a string, so you can queue an event<br />

with any name you like, and call WinJS.Application.addEventListener with that same name anywhere<br />

else in the app. This can be useful for centralizing custom events that you might invoke both during<br />

startup and at other points during execution without creating a separate global function for that<br />

purpose. It’s also a powerful means through which separately defined, independent components can<br />

raise events that get aggregated into a single handler. (For an example of using queueEvent, see<br />

Scenario 2 of the App model sample.)<br />

As for stop, this is provided to help with unit testing so that you can simulate different activation<br />

sequences without having to relaunch the app and somehow simulate the right conditions when it<br />

restarts. When you call stop, WinJS removes its listeners, clears any existing event queue, and clears the<br />

sessionState object, but the app continues to run. You can then call queueEvent to populate the queue<br />

with whatever events you like and then call start again to process that queue. This process can be<br />

repeated as many times as needed.<br />

Extended Splash Screens<br />

Now, though the default splash screen helps keep the user engaged, they won’t stay engaged if that<br />

same splash screen stays up for a really long time. In fact, “a really long time” for the typical consumer<br />

amounts to all of 15 seconds, at which point they’ll pretty much start to assume that the app has hung<br />

and return to the Start screen to launch some other app that won’t waste their afternoon.<br />

106

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

Saved successfully!

Ooh no, something went wrong!