如何在普通的JavaScript函数中调用基本的YUI3函数? [英] How do I call a basic YUI3 function from within a normal JavaScript function?

查看:91
本文介绍了如何在普通的JavaScript函数中调用基本的YUI3函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从JavaScript函数中调用一个简单的YUI3函数. 这是一些很冗长的代码,可以满足我的要求:

I'd like to call a simple YUI3 function from within a JavaScript function. Here is some code that does what I want in a very verbose way:

function changeContent (message) {
    YUI().use("node", function(Y) {
        Y.all('#content-div').setContent(message);
    });
}

有更好的方法吗?

注意:我不想将此函数附加到任何事件,我只希望有一个可用的全局changeContent()函数.

NOTE: I don't want to attach this function to any event, I just want a global changeContent() function available.

推荐答案

如果您希望API存在于YUI().use(... function(Y){/* sandbox */})之外,则您可以从YUI()捕获返回的实例.

If you want the API to exist outside of the YUI().use(...function (Y) { /* sandbox */ }), you can capture the returned instance from YUI().

(function () { // to prevent extra global, we wrap in a function
    var Y = YUI().use('node');

    function changeContent(message) {
        Y.one('#content-div').setContent(message);
    }

    ...
})();

请注意,如果您使用种子文件(yui-min.js)和动态加载器插入其他模块,则会在此处出现争用情况.可以在Node API加载并添加到Y之前调用changeContent.您可以通过预先使用组合脚本来避免这种情况.您可以从 YUI 3配置器中获取组合脚本URL.以阻塞方式提前加载模块会降低性能.您可能会或可能不会在应用程序中注意到这一点.

Be aware that there is a race condition here if you use the seed file (yui-min.js) and dynamic loader to pull in the other modules. changeContent could be called before the Node API is loaded and added to Y. You can avoid this by using a combo script up front. You can get the combo script url from the YUI 3 Configurator. There is a performance penalty for loading the modules in a blocking manner up front. You may or may not notice this in your application.

这篇关于如何在普通的JavaScript函数中调用基本的YUI3函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆