0

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

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

我最初$.fn.dropIt.settings在包装函数中使用 a 来定义设置,但后来切换到在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);
4

2 回答 2

0

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

(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'),并更新您想要的任何属性。

于 2012-07-17T00:33:57.157 回答
0

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

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

而且,像您一样,$.extendinit 方法中的默认值,但为了清楚起见,我喜欢单独声明默认值。它对我来说一直很好。

于 2012-07-16T23:57:19.263 回答