简体   繁体   中英

Migrating from YUI2 to YUI3 and domready

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM