带有选项和可访问方法的 jQuery 插件模板?

use*_*235 5 javascript jquery

我想用可访问的方法和选项构建一个插件,这是一个复杂的插件。我需要可以在插件外部访问这些方法,因为如果有人向 DOM 投放了一些广告,则需要对其进行更新(因此我们不需要再次运行完整的插件)。

我以前看到有插件可以这样做,但是我找不到它们,所以我无法查看它们。我还是 javascript 新手,所以任何帮助都会很好。

如果我们仍然可以全局覆盖这些选项,那就太好了。

我想如何使用插件:

// options
$('#someid').myplugin({name: 'hello world'});

// methods(would be nice if we can use this)
$('#someid').myplugin('update');
Run Code Online (Sandbox Code Playgroud)

// 我的旧插件包装器

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

    $.fn.pluginmyPlugin = function(options) { 

        options = $.extend({}, $.fn.pluginmyPlugin.options, options); 

            return this.each(function() {  

                var obj = $(this);

                // the code 
            });     
        };

        /**
        * Default settings(dont change).
        * You can globally override these options
        * by using $.fn.pluginName.key = 'value';
        **/
        $.fn.pluginmyPlugin.options = {
            name: '',
                            ...         
        };

})(jQuery, window, document);
Run Code Online (Sandbox Code Playgroud)

更新

因此,在查看了 jQuery 文档后,我构建了以下代码,如果代码有问题,请告诉我,如果可以构建得更好...

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

    var methods = {

        init : function( options ) {

            options = $.extend({}, $.fn.pluginmyPlugin.options, options); 

            return this.each(function(){

            alert('yes i am the main code')

            });
        },
        update : function( ) {
             alert('updated')
        }
    };

    $.fn.pluginmyPlugin = function(method) { 

        if ( methods[method] ) {
          return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
          return methods.init.apply( this, arguments );
        } else {
          $.error( 'Method ' +  method + ' does not exist on this plugin' );
        }    

    };

        /**
        * Default settings(dont change).
        * You can globally override these options
        * by using $.fn.pluginName.key = 'value';
        **/
        $.fn.pluginmyPlugin.options = {
            name: 'john doe',
            //....
        };

})(jQuery, window, document);
Run Code Online (Sandbox Code Playgroud)

Ale*_*der 3

替代:

var Plugin = function($self, options) {
  this.$self = $self;
  this.options = $.extend({}, $.fn.plugin.defaults, options);
};

Plugin.prototype.display = function(){
  console.debug("Plugin.display");
};

Plugin.prototype.update = function() {
  console.debug("Plugin.update");
};

$.fn.plugin = function(option) {
  var options = typeof option == "object" && option;

  return this.each(function() {
    var $this = $(this);
    var $plugin = $this.data("plugin");

    if(!$plugin) {
      $plugin = new Plugin($this, options);
      $this.data("plugin", $plugin);
    }

    if (typeof option == 'string') {
      $plugin[option]();
    } else {
      $plugin.display();
    }
  });
};

$.fn.plugin.defaults = {
  propname: "propdefault"
};
Run Code Online (Sandbox Code Playgroud)

用法:

$("span").plugin({
  propname: "propvalue"
});
Run Code Online (Sandbox Code Playgroud)

$("span").plugin("update");
Run Code Online (Sandbox Code Playgroud)

这与Twitter Bootstrap 的 JavaScript 模板极其相似。但是,它并没有完全从那里开始。我有很长的使用历史.data()

不要忘记适当地包裹它。