2011-04-12 8 views
1

プラグインからAPI変数を取得するにはどうすればよいですか。 jQueryのオーサリングドキュメントあたりとして、ここではプラグイン構造である:私はできるようにしたいと思います APIを使用したJqueryプラグ

(function($){ 

    var methods = { 

     init : function(options) { 

      return this.each(function() { 
       var settings = { 
        'height' : 10, 
        'width' : 10 
       }; 

       if (options) { $.extend(settings, options); } 

       $('<div>', { 
        css : { 
         height : settings.height, 
         width : settings.width, 
        } 
       }).appendTo(this); 
      }); 

     } 

    }; 

    $.fn.myPlugin = function(method) { 

     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.element'); 
     }  

    }; 

})(jQuery); 

を行うには、次の

var element = $('div').myPlugin(); 
console.log(element.properties); 

プロパティでは、私は高さを返す関数を持っているでしょうし、私のDivの幅。このデザインでプロパティ変数を作成するにはどうすればいいですか?

おかげ

あなたは取り除くでしょう

答えて

0

...

return this.each(function() { ... }); 

...とのようなプラグインのリターンの何かを持っている...しかし

return { 
    'properties': { 
     'width': calculatedWidth, 
     'height': calculatedHeight 
    } 
} 

、この意志をやって別の方法を連鎖させることはできません。

+0

ありがとう、実際私はプラグインでもっと多くのメソッドを持っています。これはほんの一例でした。理想的には、私は要素を作成するinitメソッドだけを持っていたいと思います。 'var要素= $( 'div')。myPlugin(); element.destroy(); element.getProperties(); ' – Sebastien

関連する問題