2011-08-05 4 views
0

私のプラグインは何かしばらく時間がかかります。だから、私は何かを表示することができるように、プラグインが外からそのタスクで完了したときに耳を傾けたい。jqueryプラグインの外からイベントを聴くには?

$('li').filterGroup ({ 

     onInit: function(){ 
      alert("plugin has started working"); 
     }, 

     onComplete: function(){ 

      alert("plugin has finished"); 
     } 
    }) 

私はプラグインのコード内でこれを書いてみました - -

が、私はこのような何かがしたい

theFunc is not a function 
-

(function($) { 
    // jQuery plugin definition 

    $.fn.filterGroup = function(config) 
    { 

     var settings = { 
        //some settings defined here 
         }; 




      var thisDropdown = this; //master copy 

     if(config) 
     { 
      $.extend(settings, config); 
     } 

      /*Tried following changes to make events work*/ 
      /*Tried following changes to make events work*/ 
      /*Tried following changes to make events work*/ 
      //I tried the following – assuming I will be able to listen to an onComplete event from outside 

     // reference the function from the options passed 
      var theFunc = config.onInit; 
      var anotherFunc = config.onComplete; 
      theFunc.call(); //does not work - gives error "theFunc is not a function" 

     this.each(function() 
     {  
        //here my plugin logic is done 
      anotherFunc.call(); //this is also not working - "anotherFunc is not a function" 
     }); 

     // allow jQuery chaining 
     return thisDropdown; 
    }; 

しかし、これは動作していないが、私はエラーを取得しています

注 - PLuginsのオーサリングドキュメント具体的にはイベントのセクションhttp://docs.jquery.com/Plugins/Authoring#Eventsですが、それは外から呼び出すことができる公開関数を定義するためのもので、私が探しているものではありません。これは元のjqueryドキュメントはどこですか?私はちょうど適切にドキュメントに従う必要があると思う。

は、次の質問をチェックしたが届かない -
Bind Event to Custom Plugin Function in jQuery
Custom Event For Custom JQuery Plugin
Handle events in a jQuery plugin

更新

さて、私はtheFunc.call()anotherFunc.call()を持っていた私の実際のコードではなくで、私のミスです呼び出し側の部分はonCompleteしか処理していませんでした。だから、私は明示的に私のプラグインの呼び出しでのOnInit()の部分を記述する必要はありません -

この部分 -

onInit: function(){ 
       alert("plugin has started working"); 
      } 

私はイベントは、プラグイン内で定義されている場合は、私が処理するために何をしたくない場合は意味外のコードで?

+1

あなたのコードでconsole.logを使用して、configの内容と実際にtheFuncの値を確認してください。 FirebugまたはChrome javascript consoleを使用して、何が記録されているかを確認してください。 – Joost

答えて

1

Strange。わたしにはできる。 http://jsfiddle.net/LHzjG/

ところで、config.onComplete()を使用してコールバックを直接呼び出すこともできますし、好きな場合はparamsを置くこともできます。また、あらかじめ機能があるかどうかを確認するにはjQuery.isFunction()を使用できます。

+0

ありがとう、私の間違いを持っ​​ています。私の更新を確認してください –