Banner ads. Love em or hate em? I'll leave that for others to discuss. In the meantime, how do you create a Flex 2-based, database-driven banner ad rotator?
This solution utilizes a Remote Object call to a CFC and some data-binding magic to randomly rotate the banner ad's images on a timed interval.
Where's the beef? Err... code I mean:
full
source
demo
Here's the solution that I created:
The application's creationComplete event calls the function,
getBanners
(), triggering a remote object call (associated
with the id, ROCFC) to the testcfc. The returned result,a
ColdFusion struct is handled by the my_CFC_handler function. The
struct result populates a new Object (oBanners
on the Flex-side). Next, comes a call to the
function, StartBanner(), which starts the Timer (every 5 seconds
for a total of 20 times). Each 5 seconds, the ShowBanner
()
function utilizes a Math function to randomly
select one of the banner ads returned from the CFC / database. The
image URL, the link URL, and the alt text is retrieved from the
Object, oBanners. The source attribute of the <mx:Image
source="{theImage}" .../> utilizes the bindable string,
[
Bindable
]
private
var theImage
:String
=
new String
(); and the banner ad's image randomly
changes every 5 seconds. After twenty, 5 second intervals, the
remote object method, ROCFC.GetBannerFlex
() is called again. I chose to re-call
the remote object method again in case the ads are updated in the
database.