プラグインのメソッドにパラメーターを追加する方法を探しています。
この例ではupdateメソッドを使用していますが、何を更新するかを指示するために追加のパラメーターが必要です。
//プラグインラッパー
;(function($, window, document, undefined){
var pluginName = 'myPlugin01';
function Plugin(element, options){
// vars here
};
Plugin.prototype = {
init: function(){
// init code here
},
update: function(param){
// need the param value in this method
if(param == 'bottom'){
alert('bottom it is...')
}else{
alert('top it is...')
}
},
};
$.fn[pluginName] = function(option) {
return this.each(function() {
var $this = $(this);
var data = $this.data(pluginName);
var options = typeof option == 'object' && option;
if (!data){
$this.data(pluginName, (data = new Plugin(this, options)))
}
if (typeof option == 'string'){
data[option]();
}
});
};
$.fn[pluginName].defaults = {
option1: true
};
})(jQuery, window, document);
//どのように使用したいか
$('.element').myPlugin('update','bottom');