2011-07-29 2 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 어떤 다른 사람을하지만 난 혼란 스러워요.

(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); 

유의할 것들 : 여기

답변

0

내가 그렇게 할 것입니다 방법은 jQuery를에

  1. this (특히 플러그인) 플러그인가 호출 된 객체에 대한 참조입니다. 따라서 this.html()의 내용을 반환합니다. 개체입니다. 난 당신이 $(inputTextField).val()에서 데이터를 retreive, 대신 같은 그 개체에 대한 플러그인을 호출해서는 안 위에 쓴
  2. 그래서 : this.val() 또는 $(this).val() 등의 가치에 init() 참조 다음 $(inputTextField).filterGroup(), 이전 어떤 이유로 작동하지 않을 경우 ;
  3. 더 많은 개체 데이터를 설정에 저장하려고하면 data이라는 다른 개체가 모든 요소의 데이터 목록이됩니다.
  4. 기타 : http://docs.jquery.com/Plugins/Authoring;
관련 문제