zoukankan      html  css  js  c++  java
  • 如何制作JQuery Plugin 插件【插件参数的配置】

    JQuery Plugin插件,如果大家不明白什么是JQuery插件或都不清楚如何编写可以查看其官方的网站:jQuery Authoring Guidelines

    好了,下面有一些我觉得想做一个好的插件必须应有的要求:

    1、在JQuery命名空间下声明只声明一个单独的名称

    2、接受options参数,以便控制插件的行为

    3、暴露插件的默认设置 ,以便外面可以访问

    4、适当地将子函数提供给外部访问调用

    5、保持私有函数

    6、支持元数据插件

    下面将逐条地过一遍:

    只声明一个单独的名称

    这表明是一个单独的插件脚本。如果你的脚本包含多个插件或者是互补的插件(像$.fn.doSomething()和$.undoSomething()),那么你可以根据要求声明多个名称。但一般情况下,力争用单一的名称来维持插件现实的所有细节。

    在本例中,我们将声明一个叫“hilight”的名称

    // 插件的定义 $.fn.hilight = function( options ){     // 这里就是插件的实现代码了...      };
    然后我们可以像这样调用它:
    $("divTest").hilight();

    接受一个options参数,以便控件插件的行为

    $.fn.hilight = function(options){     var defaults = {         foreground    :    'red',         background    :    'yellow'     };     //Extends out defaults options with those privided 扩展我们默认的设置     $.extend(defaults,options); };
    而我们可以这样使用它:
    $('#myDiv').hilight({   foreground: 'blue' });

    暴露插件的默认设置 ,以便外面可以访问

    作为插件的提升和优化,我们应该将上面的代码暴露出来作为插件的默认设置。

    这非常重要,这样做让使用插件的用户可以非常容易地用最少的代码重写或自定义该插件。然而这个我们可以借助JavaScript function对象的优势:

    $.fn.hilight = function(options){     //Extend our default options with those provided     //Note that the first arg to extend is an empty object     //this is to keep from overriding our "defaults" object     var opts = $.extend({},$.fn.hilight.defaults,options); } $.fn.hilight.defaults = {     foreground    :    'red',     background    :    'yellow' }; 

    这里值得注意的是$.extend()第一个参数是一个空的对象,这样可以让我们重写插件的默认设置

    用户可以像这样使用插件:

    // override plugin default foreground color $.fn.hilight.defaults.foreground = 'blue'; // ... // invoke plugin using new defaults $('.hilightDiv').hilight(); // ... // override default by passing options to plugin method $('#green').hilight({   foreground: 'green' });

    适当地将子函数提供给外部访问调用

    这个例子延续前面的例子,你会发现有一个有趣的方法可以扩展你的插件(然后还可以让其他人扩展你的插件 :))。例如,我们在插件里声明了一个函数叫“format”用来高这显示文本,我们的插件实现代码可能是这样子的:

    $.fn.hight = function(options){     //iterate and reformat each mached element     return this.each(function(){         var $this = $(this);         //...         var markup = $this.html();         //call our format function         markup = $.fn.hilight.format(markup);         $this.html(markup);     }); }; //define our format function $.fn.hilight.format = function(txt){     return '<strong>' + txt + '</strong>'; };

    保持私有函数

    暴露插件有部分内容提供重写看上去似乎非常强大,但是你必须认真地考虑你的插件哪一部分是需要暴露出来的。一旦暴露出来,你就需要考虑这些变化点,一般情况下,如果你没有把握哪部分需要暴露出来,那么你可以不这样做。

    那如何才能够定义更多的函数而不将其暴露出来呢?这个任务就交给闭包吧。为了证实,我们在插件中添加一个函数叫“debug”,这debug函数将会记录已选择的元素数量到FireBug控制台中。为了创建闭包,我们将插件定义的整部分都包装起来:

    //create closure (function($){     //plugin definition     $.fn.hilight = function(options){         debug(this);         //...     };     //private function for debuggin     function debug($obj){         if(window.console && window.console.log){             window.console.log('hilight selection count :' + $obj.size());         }     }     //...     //end of closure })(jQuery);

    这样“debug”方法就不能被闭包这外调用了

    支持元数据插件

    依赖于所写的插件类型,并支持元数据插件会使得其本身更加强大。个人来讲,我喜欢元素据插件,因为它能让你分离标签中重写插件的配置(这在写demo和示例时特别有用)。最重要的是,想现实它特别的容易!

    $.fn.hilight = function(options){     //build main options before element interation     var opts = $.extend({},$.fn.hilight.defaults,options);     return this.each(function(){         var $this = $(this);         //build element specific options         var o = $.meta ? $.extend({},opts,$this.data()) : opts;
             //一般句话,搞定支持元数据 功能            }); }

    几行的变化完成了以下几件事:

    1、检测元数据是否已经配置

    2、如果已配置,将配置属性与额外的元数据进行扩展

    <!--  markup  --> <div class="hilight { background: 'red', foreground: 'white' }">   Have a nice day!这是元数据 </div> <div class="hilight { foreground: 'orange' }">   Have a nice day!在标签中配置 </div> <div class="hilight { background: 'green' }">   Have a nice day! </div>

    然后我们通过一句脚本就可以根据元数据配置分开地高亮显示这些div标签:

    $('.hilight').hilight();

    最后,将全部代码放在一起:

    // //create closure // (function($){     //     // plugin definition     //     $.fn.hilight = function(options){         debug(this);         //build main options before element iteration         //var opts = $.extend({}, $.fn.hilight.defaults, options);         //var opts = $.extend({}, $.fn.hilight.defaults, options, $.meta ? $this.data() : {});                  // work out main settings (without metadata) - only do this work once per plugin call         var main_opts = $.extend({}, $.fn.hilight.defaults, options);                   //iterate and reformat each matched element         return this.each(function(){             $this = $(this);             //build element specific options             //var o = $.metadata  ? $.extend({}, opts, $this.metadata()) : opts;                          // if metadata is present, extend main_opts, otherwise just use main_opts               var opts = $.metadata  ? $.extend({}, main_opts, $this.metadata()) : main_opts;                           //update element styles             $this.css({                 backgroundColor: opts.background,                 color: opts.foreground             });             var markup = $this.html();         //call our format function                  });     }          //     // private function for debugging     //     function debug($obj){         if(window.console && window.console.log){             window.console.log('hilight selection count: ' + $obj.size());         }     };     //     // define and expose our format function     //     $.fn.hilight.format = function(txt){         return '<strong>' + txt + '</strong>';     };          //     // plugin defaults     //     $.fn.hilight.defaults = {         foreground    :    'red',         background    :    'yellow'     };          //     // end of clousure     // })(jQuery); 

    有网友反映为什么支持不了元数据,这是因为没有引用元数据插件,具体请查看:http://plugins.jquery.com/project/metadata

    并在本文中更新了最后的代码

    原文:http://www.learningjquery.com/2007/10/a-plugin-development-pattern/comment-page-1#comment-18346

    转载请注明出处[http://samlin.cnblogs.com/
  • 相关阅读:
    误操作 rpm -e --nodeps zlib
    Raid阵列之简单介绍
    GpG使用指南
    hadoop系统的端口
    网站日志流量复杂分析
    Flume在企业大数据仓库架构中位置及功能
    Hue的安装与部署
    Hive中的数据倾斜
    Hive的三种Join方式
    如何每日增量加载数据到Hive分区表
  • 原文地址:https://www.cnblogs.com/yuzhongwusan/p/2286867.html
Copyright © 2011-2022 走看看