Developer Network Home - Help

Yahoo! UI Library: Module

Yahoo! UI Library: Module

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.

Note: Module is fundamentally a building block for other UI controls; this document is of most use to those interested in the inner workings of controls built on Module.

Getting Started

To use Module, include the following code in your page:

If you are not using Tooltip, Panel, Dialog, or SimpleDialog, you may substitute container-min.js with the smaller container_core-min.js file:

YUI dependency configurator.

YUI Dependency Configurator:

Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader write out the full HTML for including the precise files you need for your implementation.

Note: If you wish to include this component via the YUI Loader, its module name is container. (Click here for the full list of module names for YUI Loader.)

Where these files come from: The files included using the text above will be served from Yahoo! servers; see "Serving YUI Files from Yahoo!" for important information about this service. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug versions of YUI JavaScript files, please download the library distribution and host the files on your own server.

Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.

Using "CSS skins": One of the files listed above contains a CSS "skin" for the Module Control. For this skin CSS to work correctly, you will need to apply the CSS class "yui-skin-sam" to an element that is a parent of the element in which the component lives. You can usually accomplish this simply by putting the class on the <body> tag:

<body class="yui-skin-sam">

For more information on skinning YUI components and making use of default skins, see our Understanding YUI Skins article here on the website.

Using Module

This section describes common tasks for creating and using Module. It contains these sections:

Defining Module Markup

In its most basic form, the HTML markup for a Module might look like this:

Initializing the Module

To instantiate a Module around a pre-existing piece of markup, use this code:

If the Module doesn't exist on the page, a new one will be created. Then, content (as an HTML string or as a DOM element) can be set into each section using the setHeader, setBody, and setFooter methods:

The setHeader, setBody, and setFooter methods can take either a DOM element or an HTML string as the argument. Whatever argument you pass in to these methods will entirely replace the current contents of the section you are addressing — that is, setBody will set the body section's innerHTML to "" (empty string) prior to inserting your argument as the body content.

Using Configuration Properties

Configuration properties control the look and behavior of Module instances (and its subclasses). These properties are managed by a configuration object (defined by YAHOO.util.Config) that is automatically instantiated by Module's constructor. The configuration object is accessible at runtime via its host object instance's "cfg" property. The following diagram illustrates the relationship of the configuration object and its properties with its host Container family object instance:

Illustration of the relation of the Configuration object to its host Module instance.

There are two different ways to set the initial value of configuration properties: The first is via an object literal that is passed as a second argument to the Module's constructor. Configuration properties can also be set via the configuration object's queueProperty method. In either case, these properties are not applied to the Container family object instance until it is rendered. For example:

Once rendered, a Container object instance's configuration properties are accessible at runtime through the configuration object's getProperty and setProperty methods. It is possible to listen for when the value of a specific configuration property changes by using the configuration object's subscribeToConfigEvent method. Additionally, the configuration fires a "configChanged" event that can be used to listen for changes made to the value of any configuration property. For example:

See the Configuration object's API documentation for its full list of properties and methods.

Module has the following configuration properties, which it passes on to all of its subclasses through inheritance:

Name Type Default Description
visible Boolean true Sets whether or not the Module is visible on the page (Module uses the CSS "display" property to control this).
monitorresize Boolean true Configures whether or not to create a hidden off-screen element that can be used to monitor for text size changes in the DOM.
effect Object null Object or array of objects representing the ContainerEffect classes that are active for animating the container.
appendtodocumentbody Boolean false

Specifies if the module should be rendered as the first child of document.body or appended as the last child when render is called with document.body as the "appendToNode".

Appending to the body while the DOM is still being constructed can lead to Operation Aborted errors in IE hence this flag is set to false by default.

Monitoring Events

Module and its subclasses each introduce hooks for Custom Events to which a listener can subscribe. By listening for the execution of these events, you have the ability to react to the various interesting moments that occur during the lifecycle of a Module. See the full API documentation for more information on the events that are available to listening subscribers.

Rendering the Module

Once a Module is successfully instantiated, a call to its render function is required so that all visual properties are properly applied to the Module. If the Module is already in the DOM, you can simply call:

If you have created a module scriptologically with no pre-existing markup, you must pass an element to the render function so that the Module can be inserted into the DOM:

Showing and Hiding the Module

Showing and hiding a rendered Module instance (as well as rendered instances of all Module subclasses) is very easy. You can change the visibility of the Module two ways:

Support & Community

The YUI Library and related topics are discussed on the on the ydn-javascript mailing list.

In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.

Filing Bugs & Feature Requests

The YUI Library's public bug tracking and feature request repositories are located on the YUI SourceForge project site. Before filing new feature requests or bug reports, please review our reporting guidelines.

Container Family Examples:

Other YUI Examples That Make Use of the Container Family:

More Reading about the YUI Module Control:

YUI Module on del.icio.us:

Copyright © 2008 Yahoo! Inc. All rights reserved.

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