Raymond Camden's Blog Rss

Spry's HTML Panel (2)

11

Posted in | Posted on 11-06-2007 | 4,477 views

A few days ago I blogged about Spry's HTML panel. This is a rather cool, and simple, way to load content into a region of your web page via Ajax. Kin Blas of the Spry team pointed out that I missed one of the cooler features - States. Spry's Data Set feature includes a simple way to handle error and loading states. You can use some specially named DIVs and Spry will handle hiding/showing what it needs to. Turns out - the HTML panel system supports the same thing! Let's look at an example.

First off - here is a simple HTML page, based on the versions I worked on in the earlier example:

view plain print about
1<html>
2
3<head>
4    <script src="/spryjs/SpryHTMLPanel.js" language="javascript" type="text/javascript"></script>
5    <link href="/sprycssSpryHTMLPanel.css" rel="stylesheet" type="text/css">
6    <style type="text/css">
7    .HTMLPanelLoadingContent, .HTMLPanelErrorContent {
8        display: none;
9    }
10    </style>
11</head>
12
13<body>
14
15<h2>State Test</h2>
16
17<p>
18<b>
19    <a href="htmltest.cfm?slow=likeparis" onClick="panel.loadContent(this.href); return false">Test Slow</a> /
20    <a href="htmltest.cfm?bad=likelindsey" onClick="panel.loadContent(this.href); return false">Test Error</a> /
21    <a href="htmltest.cfm" onClick="panel.loadContent(this.href); return false">Test Good</a>     
22</b>
23</p>
24
25<div id="panel">
26<p>
27    Please click something!
28</p>
29<div class="HTMLPanelLoadingContent">Plesse stand by - loading important content...</div>
30<div class="HTMLPanelErrorContent">Something went wrong. I blame Microsoft.</div>
31</div>
32
33<script type="text/javascript">
34var panel = new Spry.Widget.HTMLPanel("panel");
35</script>
36</body>
37</html>

The first thing I want to point out is 2 new CSS elements: HTMLPanelLoadingContent and HTMLPanelErrorContent. I manually set their display to none. (I'm not sure why these aren't defined in SpryHTMLPanel.css) Next look at my links:

view plain print about
1<a href="htmltest.cfm?slow=likeparis" onClick="panel.loadContent(this.href); return false">Test Slow</a> /
2<a href="htmltest.cfm?bad=likelindsey" onClick="panel.loadContent(this.href); return false">Test Error</a> /
3<a href="htmltest.cfm" onClick="panel.loadContent(this.href); return false">Test Good</a>

For my demo I linked to the same page 3 times, with different URL parameters in each one. I've got a 'slow' test, an error test, and a simple good test. Next look in my panel. I added 2 new DIVs:

view plain print about
1<div class="HTMLPanelLoadingContent">Please stand by - loading important content...</div>
2<div class="HTMLPanelErrorContent">Something went wrong. I blame Microsoft.</div>

The rest of the code is not changed. Spry will notice these 2 classes on load and when the specific state occurs (loading content, or an error), it will show and hide the divs. Pretty simple, right? The CFM code is nothing complex:

view plain print about
1<cfif structKeyExists(url, "slow")>
2    <cfset sleep(2000)>
3</cfif>
4
5<cfif structKeyExists(url, "bad")>
6    <cfthrow message="Holy errors, Batman!">
7</cfif>
8
9<cfoutput>
10Here is the CFM page. It's Business Time!
11</cfoutput>

As you can see, I simply check for the URL parameters and either intentionally slow the page down or throw an error. You can see a live demo of this here.

Comments

[Add Comment] [Subscribe to Comments]

Great example, but I have one "issue" with the content of the "good" div: Everyone KNOWS that Business Time is only on Wednesday Night. And if you don't know that, check this out:

www.youtube.com/watch?v=WGOohBytKTU

:-)

Troy
Ooops...guess I need the full URL for it to work in a comment. Let's try again:

http://www.youtube.com/watch?v=WGOohBytKTU

Troy
Is the data being loaded when the user clicks the links? or they all load at the same time (should load at the same time from our previous conversation about CF and Spry right?)

Is there anyway to dynamically create this regions? pretty much like the cflayout can? (cant see this on the livedocs)

Thanks for this site, its a great.
It loads when you click.

You can add a new DIV via JS, so sure, you could make the region on the fly if you had to.
Raul,

Although you could make the region on the fly in JS, it would be more complicated to do so, and I really do not see the benefit. Could you give a reason why or an example where it would make more sense to do so?

The beauty of this solution, and Spry in general, is that it mostly controlled via CSS styles with attached behaviors. Creating the div on the fly based on some server results or timing would be moving *slightly* away from one of the main goals of Spry.
Yeah I know simplicity is the key feature in Adobe web products (and the reason why I use CF, Flex and now Spry) but I prefer Spry over extJS (what CF uses) so if I have a contact list and I want to open each contact on a tab, that would be a good example on why I would want to implement this.

I assume I can create this regions inside the tabs. Is nothing I am working on, Im just wondering.
By tabs do yo mean Spry's tabs? I don't see why not. Don't forget when you make the new div/region/whatever, you also make a new instance of the HTMLPanel.
Yes, Spry tabs. Great...! More reasons to love Adobe.
That is quite impressive. I have not used spry but am learning jQuery. For a learning exercise I duplicated the same functionality using jQuery. While it does have ajaxStart(), ajaxStop() and ajaxError() functions that are automatically called, I still had to do the manual work to show and hide the divs accordingly.

It's nice that spry takes that next step for you and uses HTMLPanelLoadingContent and HTMLPanelErrorContent names as a convention to know those should be hidden or shown when necessary.
hi, I tried this example with a little modification, when I am using Spry on the loaded HTML Panel content (simple Json dataset and a sortable table) none of the Spry code fires, I get the "literal" values in there (variables names in curly brackets)

Loaded content on the HTML panel cant have Spry has well?
Oops

{ evalScripts: true }

[Add Comment] [Subscribe to Comments]