YUI 3.x Home -

YUI Library Examples: TabView: Loading Tab Content

TabView: Loading Tab Content

This example shows how to plug IO functionality into TabView so that data can be loaded remotely.

Creating an IO Plugin For TabView

Setting Up The YUI Instance

For this example, we'll start from the widget-io plugin created in the widget plugin example, pull in tabview and the gallery-widget-io module which provides the base class that we'll extend to create our io plugin class for TabView. The code to set up our sandbox instance is shown below:

  1. YUI({...}).use("tabview", "gallery-io-plugin", function(Y) {
  2. // We'll write our code here, after pulling in the default TabView widget
  3. // and the widget-io gallery module.
  4. }
YUI({...}).use("tabview", "gallery-io-plugin", function(Y) {
    // We'll write our code here, after pulling in the default TabView widget
    // and the widget-io gallery module.
}

TabIO Class Structure

The TabIO class will extend the Plugin.WidgetIO base class. Since WidgetIO derives from Base, we follow the same pattern we use for widgets and other utilities which extend Base to setup our new class.

  1. TabIO = function(config) {
  2. TabIO.superclass.constructor.apply(this, arguments);
  3. };
  4.  
  5. Y.extend(TabIO, Y.Plugin.WidgetIO, {
  6. initializer: function() {...}
  7. }, {
  8. NAME: 'tabIO', // component name
  9. NS: 'io' // plugin namespace
  10. });
  11.  
TabIO = function(config) {
    TabIO.superclass.constructor.apply(this, arguments); 
};
 
Y.extend(TabIO, Y.Plugin.WidgetIO, {
    initializer: function() {...}
}, {
    NAME: 'tabIO', // component name
    NS: 'io' // plugin namespace 
});
 

Lifecycle Methods: initializer, destructor

The base WidgetIO plugin implements the initializer and destructor lifecycle methods. For the purposes of this example, we will extend the TabIO plugin's initializer so that it loads or refreshes the content whenever the tab is selected.

  1. initializer: function() {
  2. var tab = this.get('host');
  3. tab.on('selectedChange', this.afterSelectedChange);
  4. },
  5.  
  6. afterSelectedChange: function(e) { // this === tab
  7. if (e.newVal) { // tab has been selected
  8. this.io.refresh();
  9. }
  10. }
  11.  
initializer: function() {
    var tab = this.get('host');
    tab.on('selectedChange', this.afterSelectedChange);
},
 
afterSelectedChange: function(e) { // this === tab
    if (e.newVal) { // tab has been selected
        this.io.refresh();
    }
}
 

Extending the Plugin

The setContent method is where we can extend the TabIO subclass to update the Tab's content.

  1. setContent: function(content) {
  2. var tab = this.get('host');
  3. tab.set('content', content);
  4. }
setContent: function(content) {
    var tab = this.get('host');
    tab.set('content', content);
}

Using the Plugin

All objects derived from Base are Plugin Hosts. They provide plug and unplug methods to allow users to add/remove plugins to/from existing instances. Plugins can also be configured during instantiation of the host instance, which is how this example works.

First thing we'll do is create a new instance of a TabView:

  1. /* Create a new TabView instance, with content generated from script */
  2. var tabview = new Y.TabView();
/* Create a new TabView instance, with content generated from script */
var tabview = new Y.TabView();

And then use the add method to add the Tab instances, to add a tab for each of the feeds, then render the tabview.

  1. var feeds = {
  2. Chrome: 'assets/news.php?query=chrome+browser',
  3. Firefox: 'assets/news.php?query=firefox+browser',
  4. Safari: 'assets/news.php?query=safari+browser',
  5. Explorer: 'assets/news.php?query=explorer+browser'
  6. };
  7.  
  8. Y.each(feeds, function(src, label) {
  9. tabview.add({
  10. label: label,
  11. plugins: [{
  12. fn: TabIO,
  13. cfg: {
  14. uri: src
  15. }
  16. }]
  17. });
  18. });
  19.  
  20. tabview.render();
  21.  
var feeds = {
    Chrome: 'assets/news.php?query=chrome+browser',
    Firefox: 'assets/news.php?query=firefox+browser',
    Safari: 'assets/news.php?query=safari+browser',
    Explorer: 'assets/news.php?query=explorer+browser'
};
 
Y.each(feeds, function(src, label) {
    tabview.add({
        label: label,
        plugins: [{
            fn: TabIO, 
            cfg: {
                uri: src
            }
        }]
    });
});
 
tabview.render();
 

Copyright © 2010 Yahoo! Inc. All rights reserved.

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