Adobe Spry

Over the weekend I finally sat down and got my hands dirty with Adobe's Spry Framework for Ajax, and I'm quite impressed. Although Spry and grown to encompass a lot of the flashy animation features common to the current batch of frameworks, Spry's roots are in a simple system for leveraging XML to create dynamic page displays, so thats where I started.

The first step in Spry is declaring a data set, and while Spry and grown to use several different data types as possible datasets, the basic is still XML. Click Here to check out the XML file I'll be using for these examples.


<script type="text/javascript" src="../../includes/xpath.js"></script>
<script type="text/javascript" src="../../includes/SpryData.js"></script>

<script type="text/javascript">
var dsCDs = new Spry.Data.XMLDataSet("/uploads/cd_catalog.xml", "/catalog/cd");
</script>

Above is a basic declaration of a new XML Data Set named dsCDs. The first input to the XMLDataSet constructor is the URL to a XML dataset, although the file need not end with .xml, in fact the target only needs to return valid XML to be used for a data set, and thus URLS like "/gallery.cfm?galleryID=2" are perfectly valid. This can also become increasingly important once you begin to declare dynamic data set URLs, but thats probably outside the scope of this brief write up. The second input is the XPath to the elements that you wish to include in your data set. Attributes and children of XML nodes selected by the given XPath query will be available in your data set, so in this case I can access the title, artist, country, company, price, and year of each CD in the catalog.

Now that we have a data set, it time to put it use it.


<body>
<ul spry:region="dsCDs">
<li spry:repeat="dsCDs">{dsCDs::artist}</li>
</ul>
</body>

What does that mean? Well the first section,


...
<ul spry:region="dsCDs">
...
</ul>
...

Declares that this is the area of the page that will be used to display the data set you've imported. The HTML within the region will be evaluated to determine the placement of values out of the dataset.

The second spry attribute usage,


...
<li spry:repeat="dsCDs">...</li>
...

Is used to tell spry that it needs to repeat that element and its children for each row in the data set. Without this tag, the <li> element would only be processed once, and thus only the first element row from the data set would be shown.

Lastly, the inner part of the <li> tag,


...{dsCDs::artist}...

Is used to tell Spry what value you want displayed, in this case the value of the XML node <artist>. Since we told Spry to repeat the <li> round this value for each row in the data set, this value will be found once for each data set, and thus produce an element in the list for each artist in the XML file.

Well, thats not a terribly useful example, and there is a lot more to Spry, but that should get you up and running if you want to play with it.

 

Comments

Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me.

Post a job. Find one. authenticjobs.com

Interested in becoming a sponsor? Contact me.