2011-08-09 7 views
2

私は画像スライダプラグインを使用していますが、その機能の一部を拡張したいと思います。あなたはjQueryのUIが行うよう、以下を呼び出すことで、次のメソッドを呼び出すことができますので、私はそれをしたいと思い:jQueryプラグイン - パブリック関数/メソッドを呼び出す

$("#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

関連する問題