私は次のように定義されたプラグインを持っています:
(function( $ ){
var mymethods = {
init: function(opts) {
// do some wild awesome magic
if (opts.drawtablefirst) $(this).drawtable(); // This doesn't actually work of course
},
drawtable: function() {
$(this).empty().append($("<table>")); // Empty table, I know...
}
}
// Trackman table
$.fn.myplugin = function(method) {
if (mymethods[method] ) {
return mymethods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method ) {
return mymethods.init.apply(this, arguments);
}
}
})( jQuery );
drawtable
メソッドからメソッドを呼び出せるようにしたいのですinit
が、その方法がうまくいきません。私は主に次のようにプラグインをインスタンス化します。
$("div#container").myplugin({drawtablefirst: true})
drawtablefirst
しかし、次のように、渡して後で手動で呼び出したくない場合があります。
$("div#container").myplugin('drawtable')
これを設定しdrawtable
てアクセス可能なプラグイン メソッドにする最善の方法は何init
ですか?
また、元の要素にdrawtable
viaでアクセスして$(this)
もうまくいかないようです。そこに適切なアプローチは何ですか?
ありがとう。