2012-04-06 2 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'를 사용하고 있는데 ... 어떻게 나타 났으며 지정한 설정 (예 : optionTwo)의 값이 무엇인지 알려주는 showSettings 메서드를 작성하는 방법은 무엇입니까?

+0

.. 아니면 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

관련 문제