04.02.2013 Views

Spry User Guide - Support - Adobe

Spry User Guide - Support - Adobe

Spry User Guide - Support - Adobe

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The spry:choose construct provides functionality equivalent to a case statement, or an if-else if-else construct. To<br />

create a spry:choose structure add a spry:choose attributetoanelement.Next,addoneormorechildelements<br />

with spry:when attributes on them. The value of a spry:when attribute should be a JavaScript expression that<br />

returns a zero or nonzero value. To have a default case, in case all of the JavaScript expressions for each spry:when<br />

attribute return zero or false, add an element with a spry:default attribute. The spry:default attribute doesn't<br />

require a value, but XHTML states that all attributes must have a value, therefore, set the value of the attribute equal<br />

to its name.<br />

The region processing engine evaluates the spry:when attribute of each node in the order they are listed under their<br />

parent element. The spry:default elementisalwaysevaluatedlast,andonlyifnospry:when expression returns a<br />

nonzero value.<br />

Region states<br />

<strong>Spry</strong> supports the notion of region states. That is, at any given time, a region is either loading data, ready to display<br />

the data, or in an error state because one or more of the data sets it is bound to failed to load its data. You can place<br />

a spry:state attribute, with a value of "loading", "error", or "ready" on elements inside aregioncontainerto<br />

associate it with a specific region state. Doing so can be quite useful for displaying a loading message as the data for<br />

aregionloads,ornotifyingtheuserthattheregionfailedtogetitsdata.Astheregionchangesstates,itautomatically<br />

regenerates its code and displays any elements with a spry:state attribute that matches the current state.<br />

The following example uses the spry:state attribute to display loading and error messages:<br />

<br />

Loading employee data ...<br />

Failed to load employee data!<br />

<br />

{firstname} {lastname}<br />

<br />

<br />

SPRY<br />

<strong>User</strong> <strong>Guide</strong><br />

Any content that does not have a spry:state attribute on it, or is not a child or descendent of an element that has<br />

a spry:state attribute on it, is always included in the output when the code is regenerated. Also, children or descendents<br />

of an element with a spry:state attributecannothavespry:state attributes. That is, nesting elements with<br />

spry:state attributes is not supported.<br />

Region observer notifications<br />

<strong>Spry</strong> supports an observer mechanism that allows a developer to register an object or function to receive a notification<br />

whenever the state of a region changes. This mechanism is almost identical to what is used for data sets with<br />

the following exceptions:<br />

• Adding and removing region observers is done through the <strong>Spry</strong>.Data.Region.addObserver() and<br />

<strong>Spry</strong>.Data.Region.removeObserver global namespaced functions. This practice is different from data sets<br />

because data set observers call addObserver() and removeObserver() methods that are on the data set object.<br />

The use of global namespaced functionsallowsadevelopertoregisteranobserverbeforethedocument'sonload<br />

event starts, and before <strong>Spry</strong> creates the JavaScript object that represents the region. Regions use addObserver<br />

and removeObserver functions because the developer might want to register observers before the JavaScript<br />

region object actually exists.<br />

• Both addObserver() and removeObserver() require an ID to identify which region the developer wants to<br />

observe. For this reason, regions that developers want to observe must have an id attribute defined on their region<br />

container node.<br />

133

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

Saved successfully!

Ooh no, something went wrong!