Javascript jQuery插件:插件方法调用其他插件方法?
我有一个插件定义如下:Javascript jQuery插件:插件方法调用其他插件方法?,javascript,jquery,Javascript,Jquery,我有一个插件定义如下: (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( $ ){
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,然后再手动调用它,比如:
$("div#container").myplugin('drawtable')
配置drawtable的最佳方法是什么?drawtable是一个可访问的插件方法,但也可以从插件方法本身(如init)中调用
另外,通过$this访问drawtable中的原始元素似乎不起作用。正确的方法是什么
谢谢。此解决方案使用jQuery ui 1.7+。小部件功能,下面是一个
$("div#container").myplugin('drawtable')
$.widget("notUi.myPlugin",{
options:{
drawtablefirst:true,
//...any other opts you want
},
_create:function(){
// do some wild awesome magic
if (this.options.drawtablefirst){
this.drawtable(); // This actually works now of course
}
},
//any function you do not put an underscore in front of can be called via .myPlugin("name", //args)
drawtable: function() {
this.element.empty().append($("<table>")); // Empty table, I know...
}
});