Fraser
Fraser

Reputation: 14246

jQuery: Adding a destroy method to plugin

I've created a plugin which I need to be able to unbind and rebind at will. How can I package this within a method in my plugin so that it can be called at will?

My plugin is like so:

 (function($) {
     $.fn.myPlugin = function(options) {
            .................
            .................
            .................
            .................
            .................
            .................
      }; 
})( jQuery );

And called like...

$('#selector').myPlugin();

Edit: Basically, I want to add a destroy method to my plugin

Upvotes: 6

Views: 5231

Answers (1)

PingOfDeath
PingOfDeath

Reputation: 137

Well somthing like that:

delete $.fn.MyPlugin;

Optionally you can write destroy method into your plugin e.g.:

destroy: function() {
    this._destroy(); //or this.delete; depends on jQuery version
    this.element.unbind( this.eventNamespace )
    this.bindings.unbind( this.eventNamespace );
    //this.hoverable.removeClass( "hover state" );
    //this.focusable.removeClass( "focus state" );
}

Upvotes: 2

Related Questions