拡張可能な jQuery プラグイン開発を学ぼうとしています。jQueryプラグインラッパーからクラス内の関数を呼び出す方法に完全に困惑しています。
私は主にhereからコードのラッパーを作成し、これを理解し/使用しようとしましたが、あまり運がありませんでした。
以下のコードの重要な部分。$(something).carousel(5)
(eg)を呼び出して、その番号をslide
Carousel クラス内の関数に渡すことができるようにしたいのです。文字列も同じなので、クラス内で$(something).carousel('go')
実行されます。go
この構造でこれを行うにはどうすればよいですか?
(function(window, $){
var Carousel = function(elem, options){
this.elem = elem;
this.$elem = $(elem);
this.options = options;
};
Carousel.prototype = {
defaults: {
[...]
},
init: function() {
this.config = $.extend({}, this.defaults, this.options);
this.create();
return this;
},
create: function() {
[...]
},
slide: function(num) {
[...]
}
};
Carousel.defaults = Carousel.prototype.defaults;
$.fn.carousel = function(options) {
return this.each(function() {
var carousel = new Carousel(this, options);
if (!options || typeof options == 'object') carousel.init();
else if (typeof options == 'number') carousel.slide(options);
else if (typeof options == 'string') carousel.options;
});
};
window.Carousel = Carousel;
})(window, jQuery);