Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Adding a destroy method to plugin

Tags:

jquery

plugins

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

like image 904
Fraser Avatar asked Oct 03 '13 13:10

Fraser


1 Answers

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" );
}
like image 54
PingOfDeath Avatar answered Oct 10 '22 09:10

PingOfDeath