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.
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:
1 | <!--Container for our call-to-action button--> |
2 | <div id="tabInsertButtonContainer"></div> |
3 | |
4 | <!--Container to which we'll append our TabView DOM--> |
5 | <div id="tabContainer"></div> |
view plain | print | ? |
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:
1 | //create our Button instance which will trigger Loader to |
2 | //load TabView and create our Tabs. |
3 | YAHOO.example.buttoninit = function() { |
4 | |
5 | //Create the button instance: |
6 | var oButton = new YAHOO.widget.Button({ |
7 | id: "tabInsertButton", |
8 | type: "button", |
9 | label: "Click here to load TabView", |
10 | container: "tabInsertButtonContainer" |
11 | }); |
12 | YAHOO.log("Button created; click button to begin loading TabView.", "info", "example"); |
13 | |
14 | //Create a handler that handles the button click; |
15 | //it logs the click, hides the button, then fires |
16 | //the function (loaderinit) that brings in TabView: |
17 | var onButtonClick = function() { |
18 | YAHOO.log("Button clicked; hiding button now and loading TabView", "info", "example"); |
19 | YAHOO.util.Dom.setStyle("tabInsertButtonContainer", "display", "none"); |
20 | YAHOO.example.loaderinit(); |
21 | } |
22 | |
23 | //attach the handler to the Button's click event: |
24 | oButton.on("click", onButtonClick); |
25 | }; |
26 | |
27 | //Once the tabInsertButtonContainer element is on the page, we can create |
28 | //our button instance; in this case, the onContentReady deferral is unnecessary, |
29 | //because we're writing the element to the page before this script, |
30 | //but in many cases the onContentReady wrapper gives you added |
31 | //flexibility and it comes at low expense: |
32 | YAHOO.util.Event.onContentReady("tabInsertButtonContainer", |
33 | YAHOO.example.buttoninit); |
view plain | print | ? |
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.
1 | YAHOO.example.loaderinit = function() { |
2 | YAHOO.log("YAHOO.example.loaderinit firing; we'll bring in TabView and any missing dependencies now.", "info", "example"); |
3 | |
4 | //Begin by creating a new Loader instance: |
5 | var loader = new YAHOO.util.YUILoader(); |
6 | |
7 | //configure Loader; we'll request TabView plus any |
8 | //optional dependencies of TabView that aren't already on |
9 | //the page: |
10 | loader.require("tabview"); |
11 | loader.loadOptional = true; |
12 | |
13 | //We can now look at the components list that Loader has |
14 | //calculated; this is what Loader has determined it needs |
15 | //to add to the page: |
16 | YAHOO.log("YUI components required: " + loader.sorted, "info", "example"); |
17 | |
18 | //We'll specify files local to the current HTML page |
19 | //so Loader does not load files from yui.yahooapis.com: |
20 | loader.base = '../../build/'; |
21 | |
22 | //When the loading is all complete, we want to initialize |
23 | //our TabView process; we can set this here or pass this |
24 | //in as an argument to the insert() method: |
25 | loader.onSuccess = YAHOO.example.tabviewinit; |
26 | |
27 | //We've created and configured our Loader instance; |
28 | //now we tell it to insert the needed components on the |
29 | //page: |
30 | loader.insert(); |
31 | |
32 | }; |
view plain | print | ? |
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:
1 | //Here's the code that will set up our TabView instance. We'll |
2 | //write this function and then tell Loader to fire it once it's done |
3 | //loading TabView into the page. |
4 | YAHOO.example.tabviewinit = function( ){ |
5 | |
6 | //Simple "tabview from javascript" syntax; pass in an id for the |
7 | //generated container element for the control, then add tabs one |
8 | //at a time. |
9 | var tabView = new YAHOO.widget.TabView( { id: 'generatedTabs' } ); |
10 | |
11 | tabView.addTab( new YAHOO.widget.Tab({ |
12 | label: 'lorem', |
13 | content: '<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat.</p>', |
14 | active: true |
15 | })); |
16 | |
17 | tabView.addTab( new YAHOO.widget.Tab({ |
18 | label: 'ipsum', |
19 | 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>' |
20 | })); |
21 | |
22 | tabView.addTab( new YAHOO.widget.Tab({ |
23 | label: 'dolor', |
24 | content: '<form action="#"><fieldset><legend>Lorem Ipsum</legend><label for="foo"> <input id="foo" name="foo"></label><input type="submit" value="submit"></fieldset></form>' |
25 | })); |
26 | |
27 | //Having created our TabView control, we append it to the DOM: |
28 | tabView.appendTo('tabContainer'); |
29 | |
30 | //Success! Log the completion of the process: |
31 | YAHOO.log("TabView instance created and appended to the DOM. The process is complete.", "info", "example"); |
32 | |
33 | }; |
view plain | print | ? |
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
INFO 2097ms (+2025) 7:54:57 PM:
LogReader instance0
LogReader initialized
INFO 72ms (+72) 7:54:55 PM:
example
Button created; click button to begin loading TabView.
INFO 0ms (+0) 7:54:55 PM:
global
Logger initialized
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings