次のコードでプラグインを作成しました。
var myplugin = {
init: function(options) {
$.myplugin.settings = $.extend({}, $.myplugin.defaults, options);
},
method1: function(par1) {
.....
},
method2: function(par1) {
.....
}
};
$.myplugin = function(method){
if ( myplugin[method] ) {
return myplugin[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if (typeof method === 'object' || !method) {
return myplugin.init.apply(this, arguments);
} else {
$.error( 'Method "' + method + '" does not exist in myplugin!');
}
};
$.myplugin.defaults = {
option1: 'test',
option2: '',
option3: ''
};
$.myplugin.settings = {};
$.myplugin();
これはうまく機能しますが、問題は、複数のオプションを設定して後でその値を返そうとすると、空になることです。1 つのオプションを設定するとうまくいきます。たとえば。最初のコンボ ボックスの値を変更する場合、次のように呼び出します: $.myplugin({option1: 'first test'}); それは機能しますが、2番目のコンボボックスで別のものを呼び出そうとすると、オプションが保存されず、代わりに空にリセットされます。
修正はありますか?