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 would be appropriate if you create custom controls for other developers to use; your desktop<br />

installation program would simply place your assets in the Addins folder. As for using such a control,<br />

when you drag and drop the control to an <strong>HTML</strong> file, its required assets (but not the icons nor the OAM<br />

file) are copied to the project into the root folder. You can then move them around however you like,<br />

patching up the file references, of course.<br />

Data Binding<br />

As I mentioned in the introduction to this chapter, the subject of data binding is closely related to<br />

controls because it’s how you create relationships between properties of data objects and properties of<br />

controls (including styles). This way, controls reflect what’s happening in the data, which is often exactly<br />

what you want to accomplish in your user experience.<br />

I want to start this discussion with a review of data binding in general, for you may be familiar with<br />

the concept to some extent, as I was, but unclear on a number of the details. At times, in fact, especially<br />

if you’re talking to someone who has been working with it for years, data binding seems to become<br />

shrouded in some kind of impenetrable mystique. I don’t at all count myself among such initiates, so I’ll<br />

try to express the concepts in prosaic terms.<br />

The general idea of data binding is again to connect or “bind” properties of two different objects<br />

together, typically a data object (or context) and a UI object, which we can generically refer to as a<br />

source and a target. A key here is that data binding generally happens between properties, not objects.<br />

The binding can also involve converting values from one type into another, such as converting a set<br />

of separate source properties into a single string as suitable for the target. It’s also possible to have<br />

multiple target objects bound to the same source object or one target bound to multiple source<br />

objects. This flexibility is exactly why the subject can become somewhat nebulous, because there are so<br />

many possibilities! Still, for most scenarios, we can keep the story simple.<br />

A common data-binding scenario is shown in Figure 4-10, where we have specific properties of two<br />

UI elements, a span and an img, bound to properties of a data object. There are three bindings here: (1)<br />

the span.innerText property is bound to the source.name property; (2) the img.src property is bound<br />

to the source.photoURL property; and (3) the span.style.color property is bound to the output of a<br />

converter function that changes the source.userType property into a color.<br />

167

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

Saved successfully!

Ooh no, something went wrong!