Migrating from YUI2 to YUI3 and domready

1.9k views Asked by At

I want to migrate the javascript in my site from YU2 to YUI3, but I am only a poor amateur programer and I am stuck at the first pitfall.

I have the following code:

MyApp.Core = function() {  
    return {  
        init: function(e, MyAppConfig) {  
            if (MyAppConfig.tabpanels) {  
                MyApp.Core.prepareTabpanels(MyAppConfig.tabpanels);  
            }  
        },  
        prepareTabpanels: function(tabpanels) {  
            // Code here
        }  
    }  
}();  

var MyAppConfig = {  
    "tabpanels":{"ids":["navigation"]}  
};

YAHOO.util.Event.addListener(window, "load", MyApp.Core.init, MyAppConfig);

How can I pass the MyAppConfig object to the MyApp.Core.init function by using YUI3 "domready" event listener?

Thanks in advance!

1

There are 1 answers

1
PottyBert On

You should be able to do something like:

var MyApp = {};
    MyApp.Core = function(){ return {  
    init: function(MyAppConfig) {  
        console.log(MyAppConfig);
    },  
        prepareTabpanels: function(tabpanels) {  
    // Code here
    }  
}  
}();

var MyAppConfig = {  
    "tabpanels":{"ids":["navigation"]}  
};

YUI().use('node', 'event', function(Y){
    Y.on('domready', MyApp.Core.init, this, MyAppConfig);
});

Note that the event is not passed in as the first parameter, it is the config.

Y.on accepts parameters as <event_type>, <callback_function>, <context>, <params>..

any parameter after the third item is passed through to the callback function so MyAppConfig becomes the first parameter in your init.

EDIT See the YUI3 API documentation here: http://developer.yahoo.com/yui/3/api/YUI.html#method_on