2011-08-09 5 views
2

나는 이미지 슬라이더 플러그인을 사용하여 일부 기능을 확장하고자합니다. 당신이 jQuery를 UI가하는 것처럼 다음을 호출하여 다음 방법을 호출 할 수 있도록 나는 그것을하고 싶습니다 :jQuery Plugin - 공용 함수/메소드 호출

$("#elemID").imageSlider("next");

나는이 작업을 수행하는 방법에 대한 손실에 가지입니다. 지금까지 제가 우주 공간에 없어 보이는 배짱이있는 곳입니다.

(function($) { 
$.fn.imageSlider = function (options) { 
    var options = $.extend({}, $.fn.imageSlider.defaults, options), 
     obj = $(this),       // Set it here so we only look for it once 
     objID = obj.attr('id'), 
     sliderName = objID + '_slider', 
     total = 0, 
     counterID = objID + '_ct'; 

    // Private Methods 
    var initialize = function() {   
     if (options.jsonObject === null) { 
      processAjax(); 
     } else { 
      process(options.jsonObject); 
     } 

     return obj; 
    } 

    // Executes an AJAX call 
    var processAjax = function() { 
     $.ajax({ 
      url: options.jsonScript, 
      type: 'GET', 
      data: options.ajaxData, 
      dataType: 'json', 
      success: function (data) { 
       process(data); 
      }, 
      complete: function (jqXHR, textStatus) { 
       if (options.onComplete !== $.noop) { 
        options.onComplete.call(); 
       } 
      } 
     }); 
    } 

    var process = function (data) { 
     // Generates the HTML 
    } 

    var changeImage = function (me, target, ops) { 
     //rotate the image 
    } 

    $.fn.imageSlider.next = function (elemID) { 
     // Currently how I call next on the slider 
    } 

    return initialize(); 
} 


$.fn.imageSlider.defaults = { 
    // options go here 
    } 
})(jQuery) 

답변

1

(see docs을)이 작업을 수행하는 표준 방법은 이름으로 저장으로 methods 당신의 방법을 각각라는 개체를 만드는 것입니다, 실제 확장자 이름으로 호출하는 방법을 검색하고 실행합니다. Like ...

(function($){ 

    var methods = { 
    init : function(options) { // THIS }, 
    process : function() { // IS }, 
    changeImage : function() { // GOOD }, 
    next : function(content) { // !!! } 
    }; 

    $.fn.imageSlider = function(method) { 

    // Method calling logic 
    if (methods[method]) { 
     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.imageSlider'); 
    }  

    }; 

})(jQuery); 
+0

고마워요! 나는 백만 번을 보았을 것임에 틀림 없다. – tomoguisuru