YUI Container: Module: Quickstart

Setting up the Modules

The Module control enables you to create a JavaScript object representation of a basic module of content. It can be used to manipulate existing content modules on your page or to create modules dynamically and append them to the DOM. All other components in the Container family have Module as their lowest-level base class.

Module is fundamentally a building block for other UI controls. The concepts presented in this example will form the basis for the way that you interact with all of its subclasses.

Module has three required dependencies: the YAHOO Global object, the Event Utility, and the DOM Collection.

In addition, the JavaScript file for Container must be included. If you will not be using any of the pre-packaged rich controls (Tooltip, Panel, Dialog, or SimpleDialog), you can include the core libary (container_core.js), which only contains Module, Overlay, and its supporting classes (Config, OverlayManager, and KeyListener). Otherwise, the full library should be included (container.js). You can see what the full list of included files looks like below. Please note that your file paths may vary depending on the location in which you installed the YUI libraries.

Modules can be built using existing markup or dynamically at runtime using JavaScript. In this tutorial, we will construct two Modules: one from markup, and one from script only. First, we'll add the Module from markup to the document. The markup is in Standard Module Format, which consists of an outer container div element and three possible child div elements that represent the header, body, and/or footer:

Next, we'll build the JavaScript for our Modules and wrap it in a function to be executed when the window is finished loading. The Module called "module1" will be associated with our existing markup; "module2" will be created dynamically from script. In this tutorial, we pass to the Module constructor the one required argument: the id associated with that Module's container element.

Note that to avoid using the global variable space, we are placing our example Modules into the YAHOO.example.container namespace. For more information about namespacing, see the YAHOO Global object.

Because "module1" is already in the document, the call to the render method requires no arguments. On the other hand, "module2" does not have any markup in the document. This means that the node where the new Module should be appended must be passed to render. In this case, we simply append "module2" to "document.body".

To see our newly created Modules more easily, we can add a style block that defines custom CSS for the "module" CSS class. By default, Module doesn't come with any predefined styles, so it is up to you as a developer to provide any applicable styles. Our style block will define the "module" CSS class such that Modules and each of their child elements will have distinct border colors:

Finally, we will add some HTML buttons to the page and wire them (using the YUI Event Utility) to the Modules' show and hide methods: