YUI 3.x Home -

YUI Library Examples: The YUI Global Object: YUI Loader - Dynamically Adding YUI and External Modules

Note: This is YUI 3.x. Looking for YUI 2.x?

The YUI Global Object: YUI Loader - Dynamically Adding YUI and External Modules

This example uses the dynamic loading capability built into YUI to pull in additional components as needed. In addition, it demonstrates how to define external modules that can be loaded alongside YUI.

This example works as follows:

  1. A YUI instance is created with a configuration object that defines parameters we need to dynamically load new modules.
  2. node is used so that we can bind an event listener to a button. YUI will dynamically fetch node and its dependencies. By default, these dependencies will be fetched from the Yahoo! CDN and will be combined into a single file.
  3. A click listener is added to a button.
  4. When this button is clicked, YUI will dynamically fetch 3.x Drag & Drop and 2.x Calendar files. The CSS file will be fetched first; this helps prevent a flash of unstyled content when the Calendar Control is loaded. This file is inserted above a style block which contains our custom calendar styles (via a YUI config option) so that styles are applied in the correct order.
  5. A Calendar instance is created, and it is made draggable.

Creating your YUI instance

First, we need to create our YUI instance with the node module, so we can attach a listener to a button.

YUI accepts a configuration object when you create an instance. Your dynamic-loading options can be defined here.

Creating the Calendar

Now that we have our core YUI instance in place, we add an event listener to a button that will dynamically load YUI 3.x Drag & Drop and YUI 2.x Calendar.

Full source

Copyright © 2009 Yahoo! Inc. All rights reserved.

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