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:
- A
YUIinstance is created with a configuration object that defines parameters we need to dynamically load new modules. nodeis used so that we can bind an event listener to a button. YUI will dynamically fetchnodeand its dependencies. By default, these dependencies will be fetched from the Yahoo! CDN and will be combined into a single file.- A click listener is added to a button.
- 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.
- 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.
