如何在YUI3中添加自定义实用程序/模块

时间:2011-11-29 15:04:44

标签: module yui3

我想在YUI3中注册一个自定义实用程序/模块,它传递一个或多个自定义节点,可以这样调用(无需实例化):

YUI().use('myCustomModule', function (Y) {
 Y.one('nodeToProcess').myCustomUtility(config);
 Y.all('manyNodes').myCustomUtility(config);
});

jQuery提供了类似的功能。

我知道YUI3中的自定义模块定义如下:

YUI.add('myCustomModule', function(Y) {
// my Code here
}, '0.1', {
requires : ['node']
});

但我不明白的是如何设置我的自定义模块,以便我可以像所描述的那样调用它。我是否必须扩展Plugin / Widget类或使用Y.Namespace()来使其工作? (见http://www.slideshare.net/caridy/building-yui-3-custom-modules

1 个答案:

答案 0 :(得分:3)

你可能想试试这个:

<!doctype html>
<head>
    <meta charset="utf-8">
    <script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script>
    <script>
YUI.add('joiz', function(Y) {

    function joizMagic(context, config) {
        alert(config.name + ' says: "' + this.getContent() + '"');
    }

    // define a function that will run in the context of a
    // Node instance:
    Y.Node.addMethod("joizMagic", joizMagic);

    // extend this functionality to NodeLists:
    Y.NodeList.importMethod(Y.Node.prototype, "joizMagic");

}, '0.1.1' /* module version */, {
    requires: ['node']
});
YUI().use('joiz', function (Y)
{
    Y.all('.message').joizMagic({ name: 'Beatrice' });
});
    </script>
</head>
<body>
    <div class="message">Hello World!</div>
    <div class="message">I'm still here!</div>
</body>
</html>