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.

This will update the target property:<br />

Here’s how we’d then create and use a reusable class for an observable object (Test 4 in the<br />

BindingTests example). Notice the object we pass to WinJS.Binding.define contains property names,<br />

but no values (they’ll be ignored):<br />

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

//...<br />

//LoginClass becomes a constructor for bindable objects with the specified properties<br />

LoginClass: WinJS.Binding.define({name: "", id: "", photoURL: "", userType: "" }),<br />

});<br />

With that in place, we can create an instance of that class, initializing desired properties. In this<br />

example, we’re using a different picture and leading userType uninitialized:<br />

var login4 = new LoginData.LoginClass({ name: "liamb",<br />

photoURL: "http://www.kraigbrockschmidt.com/images/Liam08.jpg" });<br />

Binding to this login object, we’d see that the username initially comes out black.<br />

//Do the binding (initial color of name would be black)<br />

WinJS.Binding.processAll(document.getElementById("loginDisplay"), login4);<br />

Updating the userType property in the source (as below) would then cause an update the color of<br />

the target property, which happens through the converter automatically:<br />

login4.userType = "kid";<br />

174

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

Saved successfully!

Ooh no, something went wrong!