【问题标题】:Call methods using jQuery Plugin design pattern使用 jQuery 插件设计模式调用方法
【发布时间】:2012-12-17 04:44:42
【问题描述】:

我一直在使用 jQuery Boilerplate 开发插件,但我不知道如何从插件外部调用方法。

作为参考,这里是我正在谈论的样板代码: http://jqueryboilerplate.com/

在我的小提琴中,

http://jsfiddle.net/D9JSQ/2/

代码如下:

;(function ( $, window, document, undefined ) {

    var pluginName = 'test';
    var defaults;

    function Plugin(element, options) {
        this.element = element;

        this.options = $.extend( {}, defaults, options) ;

        this._name = pluginName;

        this.init();
    }

    Plugin.prototype = {
        init: function() {
            this.hello();
        },
        hello : function() {
            document.write('hello');
        },
        goodbye : function() {
            document.write('goodbye');
        }
    }


    $.fn[pluginName] = function ( options ) {
            return this.each(function () {
                if (!$.data(this, 'plugin_' + pluginName)) {
                    $.data(this, 'plugin_' + pluginName, 
                    new Plugin( this, options ));
                }
            });
    }


})( jQuery, window, document );

$(document).ready(function() {
    $("#foo").test();
    $("#foo").test('goodbye');
});

我正在尝试使用以下语法调用再见方法:

$("#foo").test('goodbye')

我如何实现这一目标?提前致谢

【问题讨论】:

标签: jquery boilerplate


【解决方案1】:

您必须获得对该类的引用才能使用该插件结构调用它的方法。

http://jsfiddle.net/D9JSQ/3/

$(document).ready(function() {
    var test = $("#foo").test().data("plugin_test");
    test.goodbye();
});

要做你想做的事,你必须摆脱 document.write 来测试它。

http://jsfiddle.net/D9JSQ/8/

;
(function($, window, document, undefined) {

    var pluginName = 'test';
    var defaults;

    function Plugin(element, options) {
        this.element = element;

        this.options = $.extend({}, defaults, options);

        this._name = pluginName;

        this.init();
    }

    Plugin.prototype = {
        init: function(name) {
            this.hello();
        },
        hello: function(name) {
            console.log('hello');
        },
        goodbye: function(name) {
            console.log('goodbye');
        }
    }


    $.fn[pluginName] = function(options) {
        return this.each(function() {
            if (!$.data(this, 'plugin_' + pluginName)) {
                $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
            }
            else if ($.isFunction(Plugin.prototype[options])) {
                $.data(this, 'plugin_' + pluginName)[options]();
            }
        });
    }


})(jQuery, window, document);

$(document).ready(function() {
    $("#foo").test();
    $("#foo").test('goodbye');
});​

查看控制台获取信息。

【讨论】:

  • 我在某处看到了一种模式,它将所有方法存储到一个数组中,并检查调用插件时的选项是否为字符串,如果是,则调用该方法。这种结构可以吗?
  • 是的,您只需在结构中添加一个 elseif 来检查现有方法的原型。如果存在,它会被执行。这是一个很好的例子,没有必要重新发明轮子:keith-wood.name/pluginFramework.html
  • @AndrewH 我在这里回答了与 if/else 类似的问题:stackoverflow.com/questions/14078481/… 我最近注意到了这种用于开发 jQuery 插件的新模式,我会查看您发布的样板链接。
  • 谢谢 Syon,你回答了我的问题 :)
  • 这个方法可以传入参数吗?
猜你喜欢
  • 2011-10-30
  • 2011-11-24
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 2015-09-16
  • 2013-08-08
相关资源
最近更新 更多