Я пытался осмыслить хороший способ структурирования плагина, чтобы он мог принимать вызовы методов с параметрами, только вызовы методов, параметры init и init без параметров.
Пока вот что у меня есть.
(function($) {
var settings = {};
var defaults = {
args : "default args"
};
var methods = {
init : function(options) {
if(options) {
settings = $.extend({},defaults,options);
}
},
test : function(arg) {
alert("test: " + arg.args);
alert("args: " + settings.args);
}
};
$.fn.dataTable = function(method) {
var args = arguments;
var $this = this;
return this.each(function() {
if ( methods[method] ) {
return methods[method].apply( $this, Array.prototype.slice.call( args, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( $this, args );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.plugin' );
}
});
};
})(jQuery);
$(document).ready(function(){
$(".tbl").dataTable();
//$(".tbl").dataTable({ args : "hello world" });
$(".tbl").dataTable("test",{args:"test args passed"});
//$(".tbl").dataTable("test");
});
однако с этим я получаю
test: аргументы теста пройдены
и
args: undefined
Любая помощь?