jquery - 我如何向这个 jQuery 插件模式添加一个方法?

标签 jquery plugins

我使用以下代码作为我正在为一个项目开发的插件的基础 - 它来自 Smashing Magazine 上的一篇文章,位于“A Lightweight Start”标题下:

http://coding.smashingmagazine.com/2011/10/11/essential-jquery-plugin-patterns/

到目前为止,它对我的​​目的来说工作得很好,但文章的其余部分偏离了主题,讨论了哪些 jQuery UI 小部件,我认为我需要 jQuery UI 库,但我并不真正想要这些使用。

/*!
 * jQuery lightweight plugin boilerplate
 * Original author: @ajpiano
 * Further changes, comments: @addyosmani
 * Licensed under the MIT license
 */

// the semi-colon before the function invocation is a safety
// net against concatenated scripts and/or other plugins
// that are not closed properly.
;(function ( $, window, document, undefined ) {

    // undefined is used here as the undefined global
    // variable in ECMAScript 3 and is mutable (i.e. it can
    // be changed by someone else). undefined isn't really
    // being passed in so we can ensure that its value is
    // truly undefined. In ES5, undefined can no longer be
    // modified.

    // window and document are passed through as local
    // variables rather than as globals, because this (slightly)
    // quickens the resolution process and can be more
    // efficiently minified (especially when both are
    // regularly referenced in your plugin).

    // Create the defaults once
    var pluginName = 'defaultPluginName',
        defaults = {
            propertyName: "value"
        };

    // The actual plugin constructor
    function Plugin( element, options ) {
        this.element = element;

        // jQuery has an extend method that merges the
        // contents of two or more objects, storing the
        // result in the first object. The first object
        // is generally empty because we don't want to alter
        // the default options for future instances of the plugin
        this.options = $.extend( {}, defaults, options) ;

        this._defaults = defaults;
        this._name = pluginName;

        this.init();
    }

    Plugin.prototype.init = function () {
        // Place initialization logic here
        // You already have access to the DOM element and
        // the options via the instance, e.g. this.element
        // and this.options
    };

    // A really lightweight plugin wrapper around the constructor,
    // preventing against multiple instantiations
    $.fn[pluginName] = function ( options ) {
        return this.each(function () {
            if (!$.data(this, 'plugin_' + pluginName)) {
                $.data(this, 'plugin_' + pluginName,
                new Plugin( this, options ));
            }
        });
    }

})( jQuery, window, document );

我现在需要为此添加一个方法,但我对如何做到这一点非常无能为力。

该方法需要以这样的方式工作,即插件在页面上创建的实例可以通过控制台使用值调用该方法来动态更改属性(最终这将通过其他进程发生,但控制台目前还不错)。

我将如何修改上面的代码以实现此目的?还是我找错了树?

任何帮助将不胜感激,复杂的 JavaScript 有时会让我有点迷失在黑暗中,我担心,但我喜欢尝试尽可能地做“最佳实践”。

最佳答案

jQuery 文档强烈建议通过将字符串传递给主插件方法来调用插件方法。这是为了阻止 $.fn 命名空间因插件的方法而变得困惑。所以你做这样的事情:

$.fn.yourPlugin = function(options) {
    if (typeof options === "string") {
        //Call method referred to by 'options'
    } else {
        //Setup plugin as usual
    }
};

在您的模式中,您已经拥有定义方法的完美位置:Plugin.prototype。例如,要添加 changeColor 方法:

Plugin.prototype.changeColor = function(color) {
    $(this.element).css("color", color);
}

注意$(this.element)的使用。这是因为在 Plugin 构造函数中,定义了一个属性 element,并且将应用插件的元素分配给它:

this.element = element;

这是实际的 DOM 元素,而不是 jQuery 对象,因此需要对其调用 jQuery。

现在你有了一个方法,你需要添加一个机制来调用它。遵循 jQuery 文档的建议:

$.fn[pluginName] = function ( options ) {
    return this.each(function () {
        if (typeof options === "string") {
            var args = Array.prototype.slice.call(arguments, 1),
                plugin = $.data(this, 'plugin_' + pluginName);
            plugin[options].apply(plugin, args);
        } else if (!$.data(this, 'plugin_' + pluginName)) {
            $.data(this, 'plugin_' + pluginName,
            new Plugin( this, options ));
        }
    });
};

然后您可以像这样调用 changeColor 方法:

$("#example").defaultPluginName("changeColour", "red");​​​​​​​​​​​​​​​​​​​​​​​​​​​

这是一个fiddle with a working example 。您可能需要在方法调用代码周围添加一些检查,以确保插件实际上已在您调用它的元素上实例化。

关于jquery - 我如何向这个 jQuery 插件模式添加一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10978070/

相关文章:

javascript - JQuery 事件 .on ('click' 未触发

plugins - 我在我的项目中使用了 `sbt-buildinfo`,但它没有生成 BuildInfo.scala 文件

javascript - 使用 jQuery 遍历 DOM,从节点返回 HTML 属性

javascript - jQuery - 基于复选框禁用 href - 脚本位置?

jquery - 如何从 DOM 中删除 JQuery 对话框

jQuery: css z-index 不工作

plugins - Munin:当插件文件名包含点时,使用 munin.conf 更改插件类别?

plugins - ils。 Spring 安全核心。安装问题

java - 如何获取当前的maven目标版本

grails - 最佳实践,包括 Grails 中的 Angular UI