YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Carousel Control: Using Ajax for deferred loading of items

# Carousel Control: Using Ajax for deferred loading of items

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

View example in new window.

## Making the Carousel Widget to dynamically load images on the fly

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

This example has the following dependencies:

<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/2.7.0/build/carousel/assets/skins/sam/carousel.css"> <script src="http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-dom-event.js"></script> <script src="http://yui.yahooapis.com/2.7.0/build/element/element-min.js"></script> <script src="http://yui.yahooapis.com/2.7.0/build/connection/connection-min.js"></script> <script src="http://yui.yahooapis.com/2.7.0/build/carousel/carousel-min.js"></script>

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

<div id="container"> <ol id="carousel"></ol> </div> <!-- The spotlight container --> <div id="spotlight"></div>

We have a few simple CSS rules to set the dimensions for the Carousel items.

.yui-carousel-element li { height: 75px; width: 75px; }

The YUI Carousel Control's constructor is passed with the total number of items so that it triggers the `loadItems` event if the items are not available. We'll use Connection Manager to load a set of items into the Carousel as early as possible.

YAHOO.util.Event.onDOMReady(function (ev) { var carousel, spotlight = YAHOO.util.Dom.get("spotlight"); carousel = new YAHOO.widget.Carousel("container", { numItems: 17 }); YAHOO.util.Connect.asyncRequest("GET", "php/getimages.php", { success: function (o) { var i, r = eval('(' + o.responseText + ')'); curpos = r.length; for (i = 0; i < curpos; i++) { items.push(r[i]); } // check if the Carousel widget is available if (typeof carousel != "undefined") { for (i = 0; i < curpos; i++) { // if so, shove the elements into it carousel.addItem(getImageTag(items[i])); } carousel.set("selectedItem", 0); items = []; } }, failure: function (o) { alert("Ajax request failed!"); } }); });

The YUI Carousel Control 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. Apart from subscribing to this event, for displaying the selected image as a spotlight, we can subscribe to the `itemSelected` event that Carousel control exposes. This event is triggered whenever an item is selected and it returns the index of the selected item. With the index of the item, we can use the Carousel's `getElementForItem()` API to get the reference to the Carousel's item (an `li` element in our case).

carousel.on("loadItems", function (o) { // more items available? getImages.call(this); }); carousel.on("itemSelected", function (index) { var item = carousel.getElementForItem(index); if (item) { spotlight.innerHTML = "<img src=\"" + getImage(item) + "\">"; } }); carousel.render(); carousel.show(); function getImageTag(img) { return "<img src=\"" + img + "\" height=\"75\" width=\"75\">"; } function getImages() { var carousel = this; YAHOO.util.Connect.asyncRequest("GET", "assets/php/getimages.php?pos="+curpos, { success: function (o) { var i = curpos, j = 0, r = eval('(' + o.responseText + ')'); curpos += r.length; while (i < curpos) { if (r[j]) { carousel.addItem(getImageTag(r[j])); } else { break; } i++; j++; } carousel.set("selectedItem", carousel.get("firstVisible")); }, failure: function (o) { alert("Ajax request failed!"); } }); }

The getImage() function is quite easy to implement. Since we have the reference to the Carousel's item, it is straightforward to implement a function that extracts the image within it.

// Get the image link from within its (parent) container. function getImage(parent) { var el = parent.firstChild; while (el) { // walk through till as long as there's an element if (el.nodeName.toUpperCase() == "IMG") { // found an image // flickr uses "_s" suffix for small, and "_m" for big images respectively return el.src.replace(/_s\.jpg$/, "_m.jpg"); } el = el.nextSibling; } return ""; }

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### Carousel Control Examples:

- Spotlight example
- Circular Carousel example
- Partially revealing previous and next items
- Using both images and text within an item
- Using Ajax for deferred loading of items
- Using Paginator with Carousel
- Using the Carousel ARIA Plugin

### More Carousel Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings