【问题标题】:Correct place to define options in jquery plugin在 jquery 插件中定义选项的正确位置
【发布时间】:2012-07-15 21:14:24
【问题描述】:

我有一个使用命名空间(方法)的 jQuery 插件,并且还有可以在初始化时覆盖的默认选项。

我想知道在命名空间中使用这个插件来定义和使用选项的最佳方式是什么。

我最初在包装函数中使用$.fn.dropIt.settings 来定义设置,但后来切换到在init 方法中定义它们。然而,这在范围方面非常有限..

这是我插件中的相关代码

(function($, window, document, undefined){
  var methods = {
      init: function(options)
      {
        var settings = $.extend({
          trigger: "hover",
          animation: 'slide', /* none, slide, fade, grow */
          easing: 'swing', /* swing, linear, bounce */
          speedIn: 400,
          speedOut: 400,
          delayIn: 0,
          delayOut: 0,
          initCallback: function(){},
          showCallback: function(){},
          hideCallback: function(){}
        }, options);

        $(this).each(function(index, ele){
          $ele = $(ele);
          $ele.addClass('dropit');
          //Attach event handlers to each list-item
          $('li', $ele).dropIt('attach', settings);

          //If list is displayed veritcally, add extra left padding to all sub-menus
          if($(ele).hasClass('vertical'))
          {
            $('li', $ele).find('ul').addClass('nested sub-menu');
          } else {
            $('li ul', $ele).addClass('nested').find('ul').addClass('sub-menu');
          }
        });

        //Call custom callback
        settings.initCallback.call();

        //Return jQuery collection of lists
        return $(this);
      },

      attach: ...

      _trigger: ...

      _hide: ...

      }
    };

  $.fn.dropIt = function(method){
    //Variables and Options
    var $this = $(this);
    // 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.dropIt' );
    }
  };

})(jQuery, window, document);

【问题讨论】:

    标签: javascript jquery jquery-plugins namespaces


    【解决方案1】:

    阅读jQuery Plugins/Authoring页面后,我的插件结构基本上是这样的:

    (function ($) {
        var defaults = {
            // set default options
        }
        // internal functions
        var methods = {
            // plugin methods
        }
        $.fn.pluginName = function (method) {
        }
    })(jQuery);
    

    $.extend init 方法中的默认值一样,但为了清楚起见,我喜欢单独声明默认值。对我来说效果很好。

    【讨论】:

      【解决方案2】:

      我总是将设置对象设置为插件范围的全局变量,如下所示:

      (function($, window, document, undefined){
        var settings; //NOTE THIS LINE
      
        var methods = {
            init: function(options)
            {
              settings = $.extend({ //AND THIS LINE
                trigger: "hover",
                animation: 'slide', /* none, slide, fade, grow */
                easing: 'swing', /* swing, linear, bounce */
                speedIn: 400,
                speedOut: 400,
                delayIn: 0,
                delayOut: 0,
                initCallback: function(){},
                showCallback: function(){},
                hideCallback: function(){}
              }, options);
      
              $(this).each(function(index, ele){
                $ele = $(ele);
                $ele.addClass('dropit');
                //Attach event handlers to each list-item
                $('li', $ele).dropIt('attach', settings);
      
                //If list is displayed veritcally, add extra left padding to all sub-menus
                if($(ele).hasClass('vertical'))
                {
                  $('li', $ele).find('ul').addClass('nested sub-menu');
                } else {
                  $('li ul', $ele).addClass('nested').find('ul').addClass('sub-menu');
                }
              });
      
              //Call custom callback
              settings.initCallback.call();
      
              //Return jQuery collection of lists
              return $(this);
            },
      
            attach: ...
      
            _trigger: ...
      
            _hide: ...
      
            }
          };
      
        $.fn.dropIt = function(method){
          //Variables and Options
          var $this = $(this);
          // 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.dropIt' );
          }
        };
      
      })(jQuery, window, document);
      

      编辑: 你也可以做$(this).data('youPlugInName', settings),然后如果你想改变它,你可以从data('yourPlugInName')检索它,然后更新你想要的任何属性。

      【讨论】:

      • 如果这些是全局定义的,那么多次调用插件会保留第一次调用时设置的任何选项。如果我通过一次调用插件将动画设置为“淡入淡出”,然后在其他元素上再次初始化,动画仍设置为“淡入淡出”。
      • 请注意我的编辑@ChristianBenincasa - 你试过这种方法吗?你怎么看?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 2014-08-10
      • 2012-05-24
      • 2018-03-25
      • 2020-12-19
      • 1970-01-01
      相关资源
      最近更新 更多