简体   繁体   English

学习和扩展jQuery插件

[英]learning and expanding a jquery plugin

I am in process of learning more about javascript plugins and found one that is of interest to me. 我正在学习有关javascript插件的更多信息,并发现了一个我感兴趣的插件。 I am willing to get my feet dirty and see how this thing can be modified... 我愿意弄脏自己的脚,看看该如何修改...

(function( $ ){
  var methods = {
    init : function( options ) { 

     return this.each(function(){ 
     var $this = $(this),
         data = $this.data('tooltip'),
         tooltip = $('<div />', {
           text : $this.attr('title')
         });

     // If the plugin hasn't been initialized yet
     if ( ! data ) {

     console.log('still working..'  );
       /*
         Do more setup stuff here
       */

       $(this).data('tooltip', {
           target : $this,
           tooltip : tooltip
       });

     }
   });

},
show : function( ) {
  console.log('this is the show');
},
hide : function( ) { 
  // GOOD
},
update : function( content ) {
  console.log('this is the update');    
  // !!! 
    }
  };

  $.fn.tooltip = function( method ) {

// Method calling logic
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 );

ok I have 4 questions... 1.how do you initialize this plugin? 好的,我有4个问题... 1.如何初始化此插件? i keep getting 'still working..' with my console log when i try to run a random div element, $('#mtest').tooltip();. 当我尝试运行随机div元素$('#mtest')。tooltip();时,控制台日志始终保持“仍然工作”状态。

2 the init: is inside the var method, which is private, meaning I can't access init: from outside of this plugin? 2 init:在var方法内部,该方法是私有的,这意味着我无法从此插件外部访问init :? right? 对? where would i put initializing logic at since it appears to be returning options...? 由于初始化逻辑似乎正在返回选项,我该在哪里放置初始化逻辑?

3. I am confused about this part of the code... 3.我对这部分代码感到困惑...

 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' );
}    

I know its returning all the methods, but... 我知道它会返回所有方法,但是...

3a. 3a。 why write methods[method]// is looks like [method] is an array, and that looks confusing to me because I don't see an array, its a bunch of methods... 为什么写methods [method] //看起来像[method]是一个数组,这让我感到困惑,因为我看不到数组,它有很多方法...

3b. 3b。 what is the else checking for? 其他还要检查什么? or why would an error occur? 还是为什么会发生错误?

thanks for any advice on helping me fully understand this plugin! 感谢您提供帮助我全面了解此插件的建议!

I don't know what your getting at with the first question. 我不知道你对第一个问题有什么看法。 But the other questions can be solved pretty easily. 但是其他问题很容易解决。

First, lets go over 3. 首先,让我们越过3。

The code you have, and what jQuery provides in their docs, is merely a sort of "getter" between you and your methods. 您拥有的代码以及jQuery在其文档中提供的内容只是您和您的方法之间的一种“获取器”。 Instead of clustering up a namespace with all of your methods, you put your methods into an object title methods (which is instantiated on the second line of your first block of code. 无需将命名空间与所有方法组合在一起,而是将方法放入对象标题methods (在第一个代码块的第二行上实例化)。

If you look at the jQuery provided code you are asking about, its not returning methods as you've thought. 如果查看您所询问的jQuery提供的代码,那么它不是您想的那样返回的方法。 Its calling the method of the key in your methods object. 它在您的methods对象中调用 methods The first if statement says that if you call your plugin (in your case, tooltip) with a string variable, it will look up that index in the methods object and Call the function. 第一个if语句表示,如果您使用字符串变量调用插件(在您的情况下为工具提示),它将在method对象中查找该索引并调用函数。

The second else if block says that if you pass a object as a parameter OR no parameter, it will call your init method. 第二个else if块表示如果将一个对象作为参数或没有参数传递,它将调用您的init方法。 This is sort of like a custom built getter/initializer for your plugin. 这有点像为您的插件定制的getter / initializer。

So now, to answer your second question, the init method can be accessed by either calling your tooltip plugin with.. 因此,现在,要回答您的第二个问题,可以通过用。调用工具提示插件来访问init方法。

1) no parameters 1)没有参数

2) a object parameter (usually options such as {"someOption":true,"anotherOption":400} ) 2)对象参数(通常为{"someOption":true,"anotherOption":400}

3) the string 'init' as in $('#id').tooltip('init') 3) $('#id').tooltip('init')的字符串'init'

This way you can also access your show and hide methods with... 这样,您还可以使用...访问显示隐藏方法。

$('#id).tooltip('hide') ... and so forth. $('#id).tooltip('hide') ...依此类推。

You can read up on this in the jQuery docs for much more detail. 您可以在jQuery文档中阅读更多信息。 This is me merely putting it into layman's terms. 这只是我将其放在外行的术语中。

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

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