04.02.2013 Views

Spry User Guide - Support - Adobe

Spry User Guide - Support - Adobe

Spry User Guide - Support - Adobe

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The second script block links the <strong>Spry</strong>Data.js <strong>Spry</strong> data library, which is stored in a folder called includes on the<br />

server:<br />

<br />

The <strong>Spry</strong> data library depends on the XPath library, so it’s important that you always link the XPath library first.<br />

The third script block contains the statement that creates the dsSpecials data set. The cafetownsend.xml XML<br />

source file is stored in a folder called data on the server:<br />

var dsSpecials = new <strong>Spry</strong>.Data.XMLDataSet("data/cafetownsend.xml", "specials/menu_item");<br />

Note: Remember that JavaScript and XML are case-sensitive languages, so it's important that you make sure that the<br />

scripts and column names you specify are capitalized (or not capitalized) appropriately.<br />

In JavaScript the new operator is used to create objects. The <strong>Spry</strong>.Data.XMLDataSet method is a constructor in the<br />

<strong>Spry</strong> data library that creates new <strong>Spry</strong> data set objects. The constructor takes two parameters: the source of the data<br />

("data/cafetownsend.xml", in this case, a relative URL) and an XPath expression that specifies the node or nodes<br />

in the XML to supply the data ("specials/menu_item").<br />

You can also specify an absolute URL as the source of the XML data, as follows:<br />

var dsSpecials = new<br />

<strong>Spry</strong>.Data.XMLDataSet("http://www.somesite.com/somefolder/cafetownsend.xml",<br />

"specials/menu_item");<br />

SPRY<br />

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

Note: The URL you decide to use (whether absolute or relative) is subject to the browser’s security model, which means<br />

that you can only load data from an XML source that is on the same server domain as the HTML page you’re linking<br />

from. You can avoid this limitation by providing a cross-domain service script. For more information, consult your server<br />

administrator.<br />

In the preceding example, the constructor creates a new dsSpecials <strong>Spry</strong> data set object. The data set obtains data<br />

from the specials/menu_item node (specified by XPath) in the cafetownsend.xml XML file and converts the data<br />

to a flattened array of objects and properties, similar to the rows and columns of a table. (For an example of the table,<br />

see the beginning of this section.)<br />

Each data set maintains the notion of a current row.Bydefault,thecurrentrowissettothefirstrowinthedataset.<br />

Later, you can change the current row programmatically by calling the setCurrentRow() method on the data set<br />

object. For more information, see “Set or change current row” on page 124.<br />

Note: The data set contains no data after you've created it with the new JavaScript operator. To load data into the<br />

dataset, first call the data set's loadData() method, which executes a request to load the XML data. <strong>Spry</strong> regions and<br />

detail regions do this automatically for the data sets they depend on, but if you are not using one of these regions, call<br />

the loadData() method manually in your page code. This loading is asynchronous, so the data might still be<br />

unavailable if you try to access it immediately after calling loadData().<br />

<strong>Spry</strong> XML Data Set advanced examples<br />

<strong>Spry</strong> XML data sets use the XMLHTTPRequest object to asynchronously load the specified URL. When the XML data<br />

arrives, it is actually in two formats: a text format, and a document object model (DOM) tree format.<br />

For example, say that you’ve specified “/photos.php?galleryid=2000” as your data source. (This is a path to a<br />

web service that retrieves XML data).<br />

93

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

Saved successfully!

Ooh no, something went wrong!