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.

var nav = WinJS.Navigation;<br />

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

PageControlNavigator: WinJS.Class.define(<br />

// Define the constructor function for the PageControlNavigator.<br />

function PageControlNavigator (element, options) {<br />

this.element = element || document.createElement("div");<br />

this.element.appendChild(this._createPageElement());<br />

this.home = options.home;<br />

nav.onnavigated = this._navigated.bind(this);<br />

window.onresize = this._resized.bind(this);<br />

//...<br />

document.body.onkeyup = this._keyupHandler.bind(this);<br />

document.body.onkeypress = this._keypressHandler.bind(this);<br />

document.body.onmspointerup = this._mspointerupHandler.bind(this);<br />

}, {<br />

First we see the definition of the Application namespace as a container for the PageControl-<br />

Navigator class. Its constructor receives the element that contains it (the contenthost div in<br />

default.html), or it creates a new one if none is given. The constructor also receives the options declared<br />

in the data-win-options attribute of that element. The page control then appends its contents to this<br />

root element, adds a listener for the WinJS.Navigation.onnavigated event, and sets up listeners for<br />

keyboard, mouse, and resizing events. It then waits for someone to call WinJS.Navigation.navigate,<br />

which happens in the activated handler of js/default.js, to navigate to either the home page or the last<br />

page viewed if previous session state was reloaded:<br />

if (app.sessionState.history) {<br />

nav.history = app.sessionState.history;<br />

}<br />

args.setPromise(WinJS.UI.processAll().then(function () {<br />

if (nav.location) {<br />

nav.history.current.initialPlaceholder = true;<br />

return nav.navigate(nav.location, nav.state);<br />

} else {<br />

return nav.navigate(Application.navigator.home);<br />

}<br />

}));<br />

When that happens, the PageControlNavigator’s _navigated handler is invoked, which in turn calls<br />

WinJS.UI.Pages.render to do the loading, the contents of which are then appended as child elements<br />

to the navigator control:<br />

_navigated: function (args) {<br />

var that = this;<br />

var newElement = that._createPageElement();<br />

var parentedComplete;<br />

var parented = new WinJS.Promise(function (c) { parentedComplete = c; });<br />

args.detail.setPromise(<br />

WinJS.Promise.timeout().then(function () {<br />

126

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

Saved successfully!

Ooh no, something went wrong!