私は、init、close、open 関数を含む単純なプラグインを持っています。このプラグインを呼び出す html テンプレートの配列があります。特定のテンプレートについてのみ、このプラグインに少し異なる動作をさせたいと思います。たとえば、open 関数に別のクラスを追加し、閉じたときに同じクラスを削除します。それを行うエレガントな方法は何ですか?HTML の ID を見つけて、同じプラグイン内の open 関数と close 関数内で if else を実行する必要がありますか、それともより良い方法がありますか?
;(function ($, window, document, undefined) {
function Plugin(element, options) {
Window = this;
this.element = element;
this._name = pluginName;
this.init(element);
}
Plugin.prototype = {
init: function(element) {
},
close:function(e){
//removes a class and hides the element
},
open:function(element){
//adds a class and shows the element
}
}
//Extend Global jQuery (where we actually add the plugin!)
$.fn[pluginName] = function (options) {
plugin = $.data(window, 'plugin_' + pluginName);
if (!(plugin instanceof Plugin)) {
$.data(window, 'plugin_' + pluginName,
plugin = new Plugin( this, options ));
}
return $Extend(this).each(function () {
$.data(this, 'plugin_' + pluginName, plugin);
});
};
}(jQuery, window, document));