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.

Activation Code Paths<br />

As we saw in Chapter 2, new projects created in Visual Studio or Blend give you the following code in<br />

js/default.js (a few comments have been removed):<br />

(function () {<br />

"use strict";<br />

var app = WinJS.Application;<br />

var activation = Windows.ApplicationModel.Activation;<br />

app.onactivated = function (args) {<br />

if (args.detail.kind === activation.ActivationKind.launch) {<br />

if (args.detail.previousExecutionState !==<br />

activation.ApplicationExecutionState.terminated) {<br />

// TODO: This application has been newly launched. Initialize<br />

// your application here.<br />

} else {<br />

// TODO: This application has been reactivated from suspension.<br />

// Restore application state here.<br />

}<br />

args.setPromise(WinJS.UI.processAll());<br />

}<br />

};<br />

app.oncheckpoint = function (args) {<br />

};<br />

app.start();<br />

})();<br />

Let’s go through this piece by piece to review what we already learned and complete our<br />

understanding of this essential code structure:<br />

• (function () { … })(); surrounding everything is again the JavaScript module pattern.<br />

• "use strict" instructs the JavaScript interpreter to apply Strict Mode, a feature of ECMAScript 5.<br />

This checks for sloppy programming practices, like using implicitly declared variables, so it’s a<br />

good idea to leave it in place.<br />

• var app = WinJS.Application; and var activation = Windows.ApplicationMode.Activation;<br />

both create substantially shortened aliases for commonly used namespaces. This is a common<br />

practice to simplify multiple references to the same part of WinJS or WinRT.<br />

• app.onactivated = function (args) {…} assigns a handler for the WinJS.UI.onactivated event,<br />

which is a wrapper for Windows.UI.WebUI.WebUIApplication.onactivated. In this handler:<br />

• args.detail.kind identifies the type of activation.<br />

• args.detail.previousExecutionState identifies the state of the app prior to this activation,<br />

which determines whether to reload session state.<br />

103

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

Saved successfully!

Ooh no, something went wrong!