【问题标题】:Trying to learn jQuery plugin development尝试学习jQuery插件开发
【发布时间】:2011-02-04 16:57:00
【问题描述】:

所以我正在尝试学习如何基于此示例为插件实现方法集合:http://docs.jquery.com/Plugins/Authoring

我无法理解的是如何将使用插件默认值扩展的选项发送到各个方法。

我很确定任何原始选项都会发送到这里的方法:

return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));

那么如何使用默认值扩展这些参数?该示例并未真正定义如何执行此操作...

var methods = {
      init : function( options ) {

           return this.each(function(){
               $(window).bind('resize.tooltip', methods.reposition);
           });

       }
}

另外,这里是来自示例插件创作页面的代码:

    (function( $ ){

  var methods = {
     init : function( options ) {

       return this.each(function(){
         $(window).bind('resize.tooltip', methods.reposition);
       });

     },
     destroy : function( ) {

       return this.each(function(){
         $(window).unbind('.tooltip');
       })

     },
     reposition : function( ) { // ... },
     show : function( ) { // ... },
     hide : function( ) { // ... },
     update : function( content ) { // ...}
  };

  $.fn.tooltip = 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 jQuery.tooltip' );
    }    

  };

})( jQuery );

【问题讨论】:

    标签: javascript jquery plugins


    【解决方案1】:

    看起来我之前的答案比我之前想象的更接近分数。

    是的,这一行正在传递参数:

    return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    

    使用.apply(),您可以调用一个方法,更改其上下文(this 值),并为其提供一组参数而不是单个参数。如果您不知道如何传递参数,这很方便。

    所以你可以像这样分解上面的行:

      // reference the arguments passed to the plugin
    var args = arguments;
    
      // eliminate the first one, since we know that's just the name of the method
    args = Array.prototype.slice.call( arguments, 1 )
    
      // call the method that was passed, 
      //    passing along the Array of the arguments (minus the first one),
    return methods[method].apply( this, args);
    
    // Note that it is being called from the context of "this" which is the jQuery object
    

    所以如果插件是这样调用的:

    $('.someElem').tooltip('destroy', 'someArg', 'anotherArg' );
    

    代码将定位 "destroy" 方法,从 Arguments 对象中切出 "destroy",并调用 "destroy" 传递剩余参数的数组。

    【讨论】:

    • 是的!绝对是我想知道的一部分。所以剩下的唯一一件事就是何时使用插件设置的默认值扩展传入的选项(参数)。我希望这不会发生在 init 方法中,但如果发生了,如何在文字中的其他方法中访问它?
    • @Jeff:这完全取决于插件。您需要分别对待每个方法并决定它是否需要接受参数(或单个选项参数)以及是否应该具有默认值。因此,如果 init 有默认选项是有意义的,请在此处进行。如果任何其他方法需要它们自己的任何类型的参数,它们将接受它们自己的参数。这是你的意思,还是我走错了路?
    • ...我想补充一点,本教程提供了一个开发插件的特定模式的示例。这不是唯一的方法,但它是一种很好的方法,因此如果您有多个需要调用的方法,您可以将它们全部命名在一个插件下。
    • @patrick dw。好吧,我明白。我确实让我的小插件工作了。我想我试图做的太多了。通常,如果您无法弄清楚如何使某些东西起作用,则说明您做错了。但是您的 cmets 非常宝贵,因为我对 jQuery 插件的工作方式了解得更多。谢谢!
    • @patrick dw:感谢您的故障,正是我需要知道的。
    【解决方案2】:

    你使用$.extend(existingObject || {} (new object), oneObject, secondObject, nthObject)

    var mydefaults = { myDefaultArg : "foo" }
    var inputOptions = { myDefaultArg : "Somethin else" }
    
    var options = $.extend({}, mydefaults, inputOptions);
    
    options.myDefaultArg == "Somethin else";
    

    要访问或保存数据,可以使用 data 方法

    因此,如果您在插件中,“this”是 jquery 元素元素,您现在可以将数据保存到其中。

    $(this).data("pluginname.somedataname", data);
    

    并检索它

    var data = $(this).data("pluginname.somedataname");
    

    【讨论】:

    • 对了,我了解extend函数的使用方法了。我可能没有尽可能地问这个问题。我的问题更多是如何从初始 $('el').tooltip(options) 传递选项以及如何从“方法”文字中访问它们。
    最近更新 更多