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.

Referring to Templates<br />

As I noted before, when you refer to a declarative template in the FlipView or ListView controls, what<br />

you’re actually referring to is an element, not an element id. The id works because the app host creates<br />

variables with those names for the elements they identify. However, we don’t actually recommend this<br />

approach, especially within page controls (which you’ll probably use often). The first concern is that only<br />

one element can have a particular id, which means you’ll get really strange behavior if you happen to<br />

render the page control twice in the same DOM.<br />

The second concern is a timing issue. The element id variable that the app host provides isn’t created<br />

until the chunk of <strong>HTML</strong> containing the element is added to the DOM. With page controls, WinJS.UI.-<br />

processAll is called before this time, which means that element id variables for templates in that page<br />

won’t yet be available. As a result, any controls that use an id for a template will either throw an<br />

exception or just show up blank. Both conditions are guaranteed to be terribly, terribly confusing.<br />

To avoid this issue with a declarative template, place the template’s name in its class attribute:<br />

<br />

Then in your control declaration, use the syntax select("") in the options record, where<br />

is anything supported by element.querySelector:<br />

<br />

There’s more to this, actually, than just a querySelector call. The select function within the options<br />

searches from the root of its containing page control. If no match is found, it looks for another page<br />

control higher in the DOM, then looks in there, continuing the process until a match is found. This lets<br />

you safely use two page controls at once that both contain the same class name for different templates,<br />

and each page will use the template that’s most local.<br />

You can also retrieve the template element using querySelector directly in code and assign the<br />

result to the itemTemplate property. This would typically be done in a page’s ready function, as<br />

demonstrated in the Grid App project, and doing so avoids both concerns identified here because<br />

querySelector will be scoped to the page contents and will happen after WinJS.UI.processAll.<br />

Template Elements and Rendering<br />

The next interesting question about templates is, what, exactly, do we get when instantiating a WinJS.-<br />

Binding.Template? This is more or less another WinJS control that turns into an element when you call<br />

WinJS.UI.processAll. But it’s different in that it removes all its child elements from the DOM, so it never<br />

shows up by itself. It doesn’t even set a winControl property on its containing element.<br />

206

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

Saved successfully!

Ooh no, something went wrong!