2011-07-29 3 views
1

私は最初のjqueryプラグインを作成しています。私は基本的なコーディングを行いましたが、コードを内部でリファクタリングし、関数内にいくつかのセクションを配置したいと考えています。プラグインの外からこれらの関数を呼びたくないことに注意してください。私的な機能だと思います。これは、やや私が入れたいの論理である -宣言と混同jqueryプラグインの中から呼び出される関数を使用

(function($) { 

    $.fn.filterGroup = function(method) 
    { 

     var someParam,someOtherParam; //declared here and values updated inside someFunction(), anotherFunction() 


     return this.each(function() 
     { 
      //plugin code starts here 
      this.someFunction(); 


      alert(someParam); //I want updated value of someParam to be available here 

      $(inputTextField).keyup(function() 
      {//as user enters input into an input field 
       inputText = $(inputTextField).val(); 

       //call a function here on this and does some modification on it. 
          this.anotherFunction(); //move a lot of lines inside this function 

       //on subsequent keyups, I want the updated value of someOtherParam to be available here 
       alert(someOtherParam); 
      } 


     }); 


     //not sure where and how to declare these functions ... these needs to be called from inside the plugin only (private functions) 
     someFunction = function(filterText) 
     { 
      //some logic on the passed this, not sure if my sentence is correct in terms of jquery... 

      //var someParam is updated here 
      someParam = "something"; 
     } 

      anotherFunction = function(filterText) 
     { 
      //var someOtherParam is updated here 
      someOtherParam = "something"; 
     } 
    }); 

})(jQuery); 

私の質問がある -

  • どこでどのように私はthis.someFunction();のようにそれを呼び出すことができるように、私は、someFunction()を定義します。
  • そして、その後のonkeyup()イベント中に更新された値someParamを読み取ることができるようにする必要があります。

私はhttp://docs.jquery.com/Plugins/Authoring名前空間セクションをチェックするが、それは外から呼び出されるパブリック関数についてですように見えます。

はまた、これらの質問を確認 -

Using functions from inside a pluginおよび他のいくつかが、私は混乱しています。ここで

答えて

0

は、私はそれを行うだろう方法は次のとおりです。注意すべき

(function($) { 

    // this object is available only inside of this function 
    var methods = { 
     init:function(options){ 

      if(typeof options =="object") settings = $.extend(settings,options); 

      return this.each(function(){ 
       methods._someFunction(); 

       // all values are available here by private settings object 
       alert(settings.someParam); 

       $(this).keyUp(function(){ 
        methods._someOtherFunction(); 

       }); 
      }); 

     }, 
     _someFunction:function(filterText) { 
      settings.someParam = "something"; 
     }, 
     _anotherFunction:function(filterText) { 
      settings.someOtherParam = "something"; 
     } 
    } 

    // that one too 
    var settings = { 
     someParam:null, 
     someOtherParam:null 
    } 

    $.fn.filterGroup = function(method) { 
     // second condition here will disable calling functions starting from "_". 
     // ex. `$(selector).filterGroup('_someFunction');` will return error 
     if(methods[method] && method[0]!=="_") 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); 

物事:jQueryので

  1. this(特にプラグイン)プラグインが呼び出されたオブジェクトへの参照。したがってthis.html()の内容を返します。オブジェクトです。私はあなたが$(inputTextField).val()からデータを盗ん、代わりのように、これらのオブジェクトのためのプラグインを呼び出すべきではない上に書いたよう
  2. ので、:this.val()または$(this).val()としての価値をinit()参照して、その後$(inputTextField).filterGroup()、および以前は、いくつかの理由で動作しない場合;
  3. 設定に複数のオブジェクトのデータを保存する場合は、dataと呼ばれる他のオブジェクトを作成すると、すべての要素のデータのリストになります。
  4. More:http://docs.jquery.com/Plugins/Authoring;
関連する問題