YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: YUI Loader Utility (beta): Using YUI Loader to Place Additional Components on a Page

# YUI Loader Utility (beta): Using YUI Loader to Place Additional Components on a Page

In some cases, you may wish to use YUI Loader to bring additional components into a page that already contains some YUI content. In this example, we'll look at how to use YUI Loader to augment a page's existing YUI content by bringing in additional dependencies for a new component.

The current page — the YUI examples template — uses several YUI components: The Logger Control to power the logger console in the upper right corner, the Button Control to stylize our "call-to-action" buttons for launching new windows or enabling the Logger, and so on. When you click the button in the middle of the example canvas below, YUI Loader will be used to fetch an additional component, the TabView Control, and construct a tabset in the example canvas. Watch the logger console at right to see the event flow as you push the button.

## Introducing Additional YUI Components into a Page On-demand

Our task here is to use the YUI Loader Utility to introduce a new YUI component, the TabView Control, onto a page that already has some YUI content loaded on it (in this case, the Button Control and the Logger Control). This pattern is one that can be useful any time you want to perform on-demand loading — loading of components only when their need is actuated by a specific user behavior.

Our "user behavior" in this example will be the click of a YUI Button. That button, when clicked, should disappear and give way to a TabView instance that we'll construct on the fly. Before we construct it, though, we'll have to load TabView onto the page; it's not loaded as part of the initial page load.

Our markup is simple: An element for our button and an element into which we'll place our TabView DOM structure once it's created:

<!--Container for our call-to-action button--> <div id="tabInsertButtonContainer"></div> <!--Container to which we'll append our TabView DOM--> <div id="tabContainer"></div>

Next, we instantiate our Button. This step consists of (a) creating the Button Instance and (b) assigning a click handler to execute our logic to load TabView and create a TabView instance when the button is clicked:

//create our Button instance which will trigger Loader to //load TabView and create our Tabs. YAHOO.example.buttoninit = function() { //Create the button instance: var oButton = new YAHOO.widget.Button({ id: "tabInsertButton", type: "button", label: "Click here to load TabView", container: "tabInsertButtonContainer" }); YAHOO.log("Button created; click button to begin loading TabView.", "info", "example"); //Create a handler that handles the button click; //it logs the click, hides the button, then fires //the function (loaderinit) that brings in TabView: var onButtonClick = function() { YAHOO.log("Button clicked; hiding button now and loading TabView", "info", "example"); YAHOO.util.Dom.setStyle("tabInsertButtonContainer", "display", "none"); YAHOO.example.loaderinit(); } //attach the handler to the Button's click event: oButton.on("click", onButtonClick); }; //Once the tabInsertButtonContainer element is on the page, we can create //our button instance; in this case, the onContentReady deferral is unnecessary, //because we're writing the element to the page before this script, //but in many cases the onContentReady wrapper gives you added //flexibility and it comes at low expense: YAHOO.util.Event.onContentReady("tabInsertButtonContainer", YAHOO.example.buttoninit);

When the button is clicked, we first need to use YUI Loader to bring TabView onto the page. We've encapsulated that logic in `YAHOO.example.loaderinit`, which in the code above we've tied to the button's click event. Here are the commented contents of the `loaderinit` function in which we instantiate Loader, configure it, and then use its insert method to bring the TabView script and CSS dependencies into the page. Note: In this example we've loaded YUI Loader in the initial page load, so its functionality is available to us in our scripts.

YAHOO.example.loaderinit = function() { YAHOO.log("YAHOO.example.loaderinit firing; we'll bring in TabView and any missing dependencies now.", "info", "example"); //Begin by creating a new Loader instance: var loader = new YAHOO.util.YUILoader(); //configure Loader; we'll request TabView plus any //optional dependencies of TabView that aren't already on //the page: loader.require("tabview"); loader.loadOptional = true; //We can now look at the components list that Loader has //calculated; this is what Loader has determined it needs //to add to the page: YAHOO.log("YUI components required: " + loader.sorted, "info", "example"); //We'll specify files local to the current HTML page //so Loader does not load files from yui.yahooapis.com: loader.base = '../../build/'; //When the loading is all complete, we want to initialize //our TabView process; we can set this here or pass this //in as an argument to the insert() method: loader.onSuccess = YAHOO.example.tabviewinit; //We've created and configured our Loader instance; //now we tell it to insert the needed components on the //page: loader.insert(); };

We have configured Loader to call `YAHOO.example.tabviewinit` when it completes its loading of TabView and its dependencies. Loader dutifully does this, waiting for each required module to load and then executing our code to setup our TabView instance. That last piece of the puzzle looks like this:

//Here's the code that will set up our TabView instance. We'll //write this function and then tell Loader to fire it once it's done //loading TabView into the page. YAHOO.example.tabviewinit = function( ){ //Simple "tabview from javascript" syntax; pass in an id for the //generated container element for the control, then add tabs one //at a time. var tabView = new YAHOO.widget.TabView( { id: 'generatedTabs' } ); tabView.addTab( new YAHOO.widget.Tab({ label: 'lorem', content: '<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat.</p>', active: true })); tabView.addTab( new YAHOO.widget.Tab({ label: 'ipsum', content: '<ul><li><a href="#">Lorem ipsum dolor sit amet.</a></li><li><a href="#">Lorem ipsum dolor sit amet.</a></li><li><a href="#">Lorem ipsum dolor sit amet.</a></li><li><a href="#">Lorem ipsum dolor sit amet.</a></li></ul>' })); tabView.addTab( new YAHOO.widget.Tab({ label: 'dolor', content: '<form action="#"><fieldset><legend>Lorem Ipsum</legend><label for="foo"> <input id="foo" name="foo"></label><input type="submit" value="submit"></fieldset></form>' })); //Having created our TabView control, we append it to the DOM: tabView.appendTo('tabContainer'); //Success! Log the completion of the process: YAHOO.log("TabView instance created and appended to the DOM. The process is complete.", "info", "example"); };

### YUI Logger Output:

### YUI Loader Utility (beta) Examples:

- Using YUI Loader to Load the Calendar Control
- Using YUI Loader to Place Additional Components on a Page
- Using addModule to Add Custom (Non-YUI) Content with YUILoader
- Use YUILoader to list requirements for a set of components.
- Complex Application (included with examples for Layout Manager)

### More YUI Loader Utility (beta) Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

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