2011-08-05 3 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 콘솔을 사용하여 기록중인 내용을 확인하십시오. – Joost

답변

1

이상한. 그것은 나를 위해 작동합니다.

BTW의 경우 config.onComplete()을 사용하여 콜백을 직접 호출하고 u가 좋아하면 매개 변수를 넣을 수도 있습니다. 그리고 jQuery.isFunction()을 사용하여 미리 기능이 있는지 확인할 수 있습니다.

+0

고마워, 내 실수있어. 내 업데이트를 확인하십시오. –

관련 문제