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.

If you were paying attention at the end of Chapter 4, you’ve probably guessed that the template is<br />

an instance of WinJS.Binding.Template. And you’re right! That piece of markup, in fact, comes just<br />

before the control declaration in html/simpleFlipview.html.<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Note that a template must always be declared in markup before any controls that reference them:<br />

WinJS.UI.processAll must instantiate the template first because the collection control will be asking<br />

the template to render its contents for each item in the data source. Also remember from Chapter 4 that<br />

instantiating a template removes its contents from the DOM so that it cannot be altered at run time.<br />

You can see this when running the sample: expand the nodes in Visual Studio’s DOM Explorer or Blend’s<br />

Live DOM pane, and you’ll see the root div of the template but none of its children.<br />

In the sample, the prosaically named ItemTemplate is made of an img element and another div<br />

containing an h2. The overlay class on that latter div, if you look at Figure 5-1 carefully, is clearly styled<br />

with a partially transparent background color (see css/default.css for the .overlaidItemTemplate<br />

.overlay selector). This shows that you can use any elements you want in a template, including other<br />

WinJS controls. In the latter case, these are picked up when WinJS.UI.process/ processAll is invoked<br />

on the template. 31<br />

You can also see that the template uses WinJS data-binding attributes, where the img.src, img.alt,<br />

and h2.innerText properties are bound to data properties called picture and title. This shows how<br />

properties of two target elements can be bound to the same source property. (Remember that if you’re<br />

binding to properties of the WinJS control itself, rather than its child elements, those properties must<br />

begin with winControl.)<br />

For the data source, the FlipView’s itemDataSource option is assigned the value of<br />

DefaultData.bindingList.dataSource that you can find in js/DefaultData.js:<br />

var array = [<br />

{ type: "item", title: "Cliff", picture: "images/Cliff.jpg" },<br />

{ type: "item", title: "Grapes", picture: "images/Grapes.jpg" },<br />

{ type: "item", title: "Rainier", picture: "images/Rainier.jpg" },<br />

{ type: "item", title: "Sunset", picture: "images/Sunset.jpg" },<br />

{ type: "item", title: "Valley", picture: "images/Valley.jpg" }<br />

];<br />

var bindingList = new WinJS.Binding.List(array);<br />

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

31 Note that for such controls to be fully interactive, assign the win-interactive class to them, otherwise the surrounding<br />

control (and this applies to ListView as well) will swallow input events before they reach those controls.<br />

182

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

Saved successfully!

Ooh no, something went wrong!