2012-04-06 3 views
1

私はずっと前に書いたシンプルな(軽量の)divスライダーを持っていました。私は様々なプロジェクトでそれを使用しているので、自分のプラグインにラップする時が来ました(軽量ではないもう!)。カスタムJQueryプラグイン - ユーザー定義のオプションを取得/維持する方法?

これは私のプラグインチェリーをポップしているので、私は読んでいて、http://docs.jquery.com/Plugins/Authoringからコピーしています。それは理にかなっていますが、何かが私を逃しています。ここに私のプラグインのコードは次のとおりです。ここ

(function($) { 

    //List of callable methods 
    var methods = { 
     init : function(options) { 

      var settings = $.extend({ 

       optionOne : 'aaa', 
       optionTwo : 'bbb', 
       optionThree : 'ccc', 
       optionFour : 'ddd' 

      }, options);   

     }, 

     error : function(message) { 
      alert(message);    
     },   

     showSettings : function() { 
      //This bit... how to show whats in 'settings' defined in init? 
     } 

    } 

    $.fn.ccSlider = function(method) { 

     //Calling methods 
     if (methods[method]) { 
      //If plugin is called with a recognised method, run that. 
      return methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1)); 
     } else if (typeof method === 'object' || !method) { 
      //if plugin is called without a method, run the init() function 
      return methods.init.apply(this, arguments); 
     } else { 
      //Run the error() function to catch all else. 
      return methods.error("ccSlider: Method '"+method+"' does not exist!");    
     } 
    }; 

})(jQuery); 

すべてが期待どおりに動作しているが、私は必要なメソッドを記述するために行くことはできませんが、私は設定」のコンテンツにアクセスする方法を見つけ出すことができないだけの理由'init()メソッドの外側にあります。

私は 'showSettings'をテストとして使用していました...どのように私はshowSettingsメソッドをポップアップし、指定された設定の値(optionTwoなど)を教えてくれましたか?

+0

..Orのベストプラクティス、jQueryのウェブサイト...私は考え出したが、私はちょうどそれが働いて得ることができなかったものだ – Codecraft

答えて

1

あなたはアーキテクチャが適切ですが、メソッドと設定は両方ともプラグインにとってグローバルでなければなりません。

(function($) { 
    var defaults = { 
     optionOne : 'aaa', 
     optionTwo : 'bbb', 
     optionThree : 'ccc', 
     optionFour : 'ddd' 
    } //if there are any 

    var settings = {}; //you can reach the settings from any where in your plugin now 
    //List of callable methods 
    var methods = { 
     init : function(options) { 
     //initialize the settings in your init function and you are good to go 
     settings = $.extend({},defaults,options);   
     }, 

     error : function(message) { 
     alert(message);    
     },   

     showSettings : function() { 
     //This bit... how to show whats in 'settings' defined in init? 
     } 

    } 

    //your plugin code here like in your post but now you can use the settings variable  we defined in your plugin 
}); 
+0

に従って。私は休憩を取って、それに戻って来て、それを行って、感謝:) – Codecraft

関連する問題