简体   繁体   English

抽象化jQuery插件创建

[英]Abstractize jQuery plugin creation

Currently I'm building my jQuery plugins using this "template": 目前,我正在使用此“模板”构建jQuery插件:

;(function($, window, document){

  var defaultOptions = {
        option1: value1,
        option2: value2,
      };

  function plugin(el, options){

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

    this.__construct();
  };

  plugin.prototype = {

    __construct: function(){
      // do the plugin stuff, set up events etc.
    },

    __destruct: function(){   
      $(this.el).removeData('myPlugin');
    },

    // other plugin functions here...
  };

  $.fn.myPlugin = function(options){
    var additionalArguments = Array.prototype.slice.call(arguments, 1);

    return this.each(function(){
      var inst = $.data(this, 'myPlugin');

      if(!(inst instanceof plugin)){
        var inst =  new plugin(this, options);
        $.data(this, 'myPlugin', inst); 
        return inst;
      }  

      if(typeof options == 'string'){
        inst[options].apply(inst, additionalArguments);
      }  
    });
  };

  $(document).ready(function(){
    $('.my-plugin').myPlugin();  
  });

})(jQuery, window, document);

I got most of the ideas from https://github.com/jquery-boilerplate/jquery-boilerplate/blob/master/src/jquery.boilerplate.js 我从https://github.com/jquery-boilerplate/jquery-boilerplate/blob/master/src/jquery.boilerplate.js获得了大部分想法

So this example does nothing really, it's just the "backbone" of the plugin, and as you can see it's quite a bit of code.... 因此,此示例实际上没有执行任何操作,仅是插件的“主干”,并且您可以看到它是很多代码。

Can I build some kind of plugin creator function that would allow me to rewrite the code above into something smaller like: 我可以构建某种插件创建器功能,使我可以将上面的代码重写为更小的代码吗:

createPlugin('myPlugin', {

  defaultOptions: {},

  __construct: function() { 
    ...
  },

  __destruct: function() { 
    ...
  },

  somePublicFunction: function(){
    ...
  }

});

But still be able to use it like 但是仍然可以像

$('.element').myPlugin();

?

In PHP I would use abstract classes for this kind of things, but I'm not sure how to do it in javascript... 在PHP中,我将使用抽象类来进行此类操作,但是我不确定如何在javascript中进行操作...

This is the plugin pattern: 这是插件模式:

(function ($){

  $.fn.myPlugin = function (options){
    // do something or not to do
    // anyway it will work
    return(this);//because 'this' will return the input selector 
  };

})(jQuery);

This enough code to achieve basic functionality. 这足够的代码来实现基本功能。

If you need some createjQueryPlugin(name, methods) function, than YES , you can. 如果需要一些createjQueryPlugin(name, methods)函数,可以使用YES Just wrap the $.fn.myPlugin in your function definition, but you still have to define namespace: 只需将$.fn.myPlugin包装在函数定义中,但仍然必须定义名称空间:

(function ($){    
//...
})(jQuery);

And finally, your code will be the same, but longer and redundant. 最后,您的代码将相同,但是更长且多余。

A plugin can be as simple or complex as you want it to be. 插件可以像您想要的那样简单或复杂。 The boilerplate template is a solid enough framework that a very robust API can be developed using it, including changing option settings on the fly. 样板模板是一个足够坚固的框架,因此可以使用它来开发非常健壮的API,包括即时更改选项设置。

As with all javascript code, their are lots of different styles for writing plugins. 与所有javascript代码一样,它们用于编写插件的样式很多。 You aren't bound to any guidelines or rules. 您不受任何准则或规则的约束。 The only absolute rule is to return this if you want plugin to be chain able. 唯一的绝对规则是,如果您希望插件具有链接能力,则返回this After that everything else within the boilerplate is optional. 之后,样板中的其他所有内容都是可选的。 What you put between the the opening and closing braces can be anything you want that suits your needs or coding style 放在开括号和闭括号之间的内容可以是您想要的任何适合您的需要或编码样式的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM