Migrating from YUI2 to YUI3 and domready

2019-05-31 03:11发布

问题:

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:

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



标签: yui3 domready