Online Book Reader

Home Category

AJAX In Action [62]

By Root 4188 0
is often referred to as outlook bar, having been popularized by its use in Microsoft Outlook.) Let’s build a simple Rico Accordion widget. Initially, we require a parent DOM

element; each child of the parent will become a pane in the accordion. We define a DIV element for each panel, with two further DIVs inside that, representing the header and the body of each panel:

Dictionary Definition

  • n.A portable wind instrument with a small keyboard and free metal reeds that sound when air is

    forced past them by pleated bellows operated by the

    player.

  • adj.Having folds or bends like the bellows Licensed to jonathan zheng

    110

    CHAPTER 3

    Introducing order to Ajax

    of an accordion: accordion pleats; accordion blinds.

A picture

The first panel provides a dictionary definition for the word accordion and the second panel a picture of a monkey playing an accordion (see figure 3.9). Rendered as it is, this will simply display these two elements one above the other. However, we have assigned an ID attribute to the top-level DIV element, allowing us to pass a reference to it to the Accordion object, which we construct like this: var outer=$('myAccordion');

outer.style.width='320px';

new Rico.Accordion(

outer,

{ panelHeight:400,

expandedBg:'#909090',

collapsedBg:'#404040',

}

);

The first line looks rather curious. $ is actually a valid JavaScript variable name and simply refers to a function in the core Prototype library. $() resolves DOM nodes in a way similar to the x library’s xGetElementById() function that we discussed in the previous section. We pass a reference to the resolved DOM element to the Accordion object constructor, along with an array of options, in the standard idiom for Prototype-derived libraries. In this case, the options simply provide some styling of the Accordion widget’s visual elements, although callback handler functions to be triggered when panels are opened or closed can also be passed in here. Figure 3.9 shows the effect of styling the DOM elements using the Accordion object. Rico’s Behaviors provide a simple way of creating reusable widgets from common markup and also separate the content from the interactivity. We’ll explore the topic of applying good design principles to the JavaScript UI in chapter 4. The final feature of the Rico framework to mention is that it provides very good support for Ajax-style requests to the server, through a global Rico AjaxEngine object. The AjaxEngine provides more than just a cross-browser wrapper around the XMLHttpRequest object. It defines an XML response format that Licensed to jonathan zheng

Third-party libraries and frameworks

111

Figure 3.9 The Rico framework Behaviors allow plain DOM nodes to be styled as interactive widgets, simply by passing a reference to the top-level node to the Behavior object’s constructor. In this case, the Accordion object has been applied to a set of DIV elements (left) to create an interactive menu widget (right), in which mouse clicks open and close the individual panels.

consists of a number of elements. The engine will automatically decode these, and it has built-in support for two types of response: those that directly update DOM elements and those that update JavaScript objects. We’ll look at a similar mechanism in greater detail in section 5.5.3, when we discuss client/server interactions in depth. For now, let’s move on to the next type of framework: one that spans both client and server.

3.5.3 Application frameworks

The frameworks that we have looked at so far are executed exclusively in the browser and can be served up as static JavaScript files from any web server. The final category of frameworks that we will review here are those that reside on the server and generate at least some of the JavaScript code or HTML markup dynamically. These are the most complex

Return Main Page Previous Page Next Page

®Online Book Reader