Photo credits: Matt Steen

To see more recent blog posts please visit http://www.sanative.net/blog


Spry Observers

So I am working on a project and have implemented Spry for a specific portion of the site. The effect is really nice, loading events for each day of the week as you click the day name without a page refresh. Since there are images and text involved I thought it would be very cool to have a fade out/fade in effect as each day was selected.

The problem I experienced was trying to get the fade in and fade out effect to work properly. I was getting a flickering effect because the two events were happening within the same function with no notion of when the data was actually loaded. Then Ray Camden point out to me that Spry has observers. These observers can be set to watch either a data set or a region and can be fired on a specific event.

So the trick was to fire off my fade out effect and an observer call when I made the data call. Once the region had changed I fired the fade in effect and removed the observer. This worked very nicely!

Here's an example:

[script]   var myevents = new Spry.Data.XMLDataSet("http://#cgi.SERVER_NAME#/path/someCFC.cfc?method=getEvents", "/events/event",{ useCache: false });
      
   var eventsObserver = new Object;
   
   function updateevents(LinkDate){
         Spry.Effect.AppearFade('eventsregion', {duration: 100, from: 100, to: 0, toggle: false});
         myevents.setURL("http://#cgi.SERVER_NAME#/path/someCFC.cfc?method=getEvents&LinkDate="+LinkDate);
         myevents.loadData();
         Spry.Data.Region.addObserver('eventsregion', eventsObserver);
   }
   
   eventsObserver.onPostUpdate = function(notifier, data) {
      Spry.Effect.AppearFade('eventsregion', {duration: 100, from: 0, to: 100, toggle: false});
      Spry.Data.Region.removeObserver('eventsregion', eventsObserver);
   };
[/script]

<div id="eventsregion" spry:region="myevents" class="SpryHiddenRegion">
   <div spry:repeat="myevents">{myevents::eventtitle}</div>
</div>

Also, note that this example is probably not best practice. If an effect is taking place when another effect is called you may experience weirdness. Ray Camden suggested I look to set a flag if the effect is still running and wait to run the new effect until after the current effect has finished processing... maybe Ill post on that soon!

Comments
Cristian MARIN's Gravatar The next version of Spry will bring a complete reshape of the Spry Effects that include notifiers and flags that show you the position of the running effect. Using these methods you can cancel a running effect easily while you start another one from a different location.

Stay tunned on Adobe Labs as these days we will release a preview of Spry 1.5 with the new functionalities included. We addressed into the new Demo of the Spry Effects a problem similar to yours.

Cristian MARIN
# Posted By Cristian MARIN | 3/14/07 7:14 AM
TJ Downes's Gravatar Thanks Cristian, that rocks!
I'm doing a Spry preso for the CFUG here next month, this will be an excellent feature to show.
# Posted By TJ Downes | 3/14/07 7:24 AM
BlogCFC 5.8.001 © Ray Camden