Carousel Widget: Dynamic Loading Example

This example showcases the dynamic loading capabilities of the YUI Carousel Widget. The YUI Carousel Widget exposes an event named "loadItems". This event is fired whenever the Carousel needs items to be loaded into it for display. Subscribing to this event makes it easy for dynamically loading the next set of images.

In this example, you can use arrow keys to select items, as well as click on an item to select it. This feature is a built-in functionality of the YUI Carousel widget.

Making the Carousel Widget to dynamically load images on the fly

Here we will use the YUI Carousel Widget's "loadItems" event to dynamically load the images on the fly.

This example has the following dependencies:

Initially we use the Connection library to load the initial set of items as soon as part of the DOM is visible.

We'll have the CSS rules to set the dimensions for the Carousel items.

We'll use the YUI Connection library to load a set of items into the Carousel as early as possible.

Let us invoke the Carousel's constructor. The YUI Carousel Widget's constructor is passed with the total number of items so that it triggers the "loadItems" event if the items are not available.

The YUI Carousel Widget exposes a "loadItems" custom event that is fired when the Carousel needs more items to be loaded. This becomes very handy for us since we can subscribe to it and add more items in to the Carousel widget when required.

In our case, the server program returns an array (JSON) of images. This is parsed in the Ajax callback and then the Carousel's addItem() is called for every image.

Putting it altogether, the full JavaScript reads as follows: