Migrating from YUI2 to YUI3 and domready

有些话、适合烂在心里 提交于 2019-12-08 08:01:39

问题


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



来源:https://stackoverflow.com/questions/4873808/migrating-from-yui2-to-yui3-and-domready

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!