0

名前空間 (メソッド) を使用する 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);
4

2 に答える 2

0

jQueryプラグイン/オーサリングページを読んだ後、私は基本的に次のようにプラグインで構成しました。

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

そして、あなたが持っているように$.extend、initメソッド内のデフォルトですが、わかりやすくするために、デフォルトを個別に個別に宣言したままにしておきたいと思います。それは私にとってうまくいっています。

于 2012-07-16T23:57:19.263 に答える
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 に答える