首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用getter方法实现jQuery插件?

如何使用getter方法实现jQuery插件?
EN

Stack Overflow用户
提问于 2012-12-24 18:37:48
回答 2查看 778关注 0票数 1

我开始创建我的自定义jQuery插件来管理标签。我正在使用一种基于模式https://github.com/zenorocha/jquery-plugin-patterns/blob/master/patterns/jquery.basic.plugin-boilerplate.js的方法,所有的方法都维护链接性。

我想要创建一个类似于$.css()的方法,它允许设置或获取值。我已经实现了一个‘tag’方法,它添加新的标签,或者,如果传递的参数是未定义的,它会返回一个数组:

代码语言:javascript
运行
复制
$(elem).tagger('tags', 'some, tags, here'); // To add more tags
$(elem).tagger('tags');  // This doesnt works because chainability

第二个调用不起作用,只是返回一个在选择器中包含所选元素的数组(因为是链式模式)。

如何实现getter方法?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-24 22:15:39

如果没有看到您的插件代码,我只能提供以下模拟代码作为答案:

代码语言:javascript
运行
复制
$.fn.myVersatilePlugin = function (options, values) {
    if (values === undefined) {
        //do something to element with given `values`
        return this; // <-- chainability
    } else {
        values = {};
        // do whatever it is you do to get what you want to return
        return values; // and return it
    }
};
票数 2
EN

Stack Overflow用户

发布于 2012-12-25 00:24:28

好了,我想我找到了我的问题的解决方案。接下来是代码。

我区分了三种可能性:

必须创建并初始化

  • 插件。调用
  • 插件的方法。调用标记为getter的
  • 插件的方法。在本例中,我中断了链接性并返回方法results。

这是我的第一个jQuery插件。有人能回答我这是一个好的解决方案还是坏的解决方案吗?提前谢谢。

代码语言:javascript
运行
复制
var pluginName = 'tagger';

//
// Plugin wrapper around the constructor,
// preventing against multiple instantiations and allowing any
// public function (whose name doesn't start with an underscore) to be 
// called via the jQuery plugin:
// e.g. $(element).defaultPluginName('functionName', arg1, arg2)
//
$.fn[pluginName] = function(options) {
    var args = arguments;

    if (options === undefined || typeof options === 'object') {
        // Create a plugin instance for each selected element.
        return this.each(function() {
            if (!$.data(this, 'plugin_' + pluginName)) {
                $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
            }
        });
    } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
        // Call a pluguin method for each selected element.
        if (Array.prototype.slice.call(args, 1).length == 0 && $.inArray(options, $.fn[pluginName].getters) != -1) {
            // If the user does not pass any arguments and the method allows to
            // work as a getter then break the chainability
            var instance = $.data(this[0], 'plugin_' + pluginName);
            return instance[options].apply(instance, Array.prototype.slice.call(args, 1));
        } else {
            // Invoke the speficied method on each selected element
            return this.each(function() {
                var instance = $.data(this, 'plugin_' + pluginName);
                if (instance instanceof Plugin && typeof instance[options] === 'function') {
                    instance[options].apply(instance, Array.prototype.slice.call(args, 1));
                }
            });
        }
    }
};

//
// Names of the pluguin methods that can act as a getter method.
//
$.fn[pluginName].getters = ['tags'];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14020187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档