Weblurk
Weblurk

Reputation: 6812

How can I destroy/remove an instance of a jQuery plugin?

I'm using sidr, a jQuery plugin for sliding in side menus. It offers a couple of public methods, and I want to extend it with a "destroy" method, which would remove the instance of the plugin.

I want to be able to call $.sidr('destroy', mySidrInstance) just as I already can call $.sidr('open', mySidrInstance)

Here are the public methods

    // Sidr public methods
var methods = {
    open: function(name, callback) {
        privateMethods.execute('open', name, callback);
    },
    close: function(name, callback) {
        privateMethods.execute('close', name, callback);
    },
    toggle: function(name, callback) {
        privateMethods.execute('toggle', name, callback);
    },
    destroy: function() {
        // I want to create this. What should I write here?
    }
};

$.sidr = function( method ) {

    if ( methods[method] ) {
        return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    }
    else if ( typeof method === 'function' || typeof method === 'string' || ! method ) {
        return methods.toggle.apply( this, arguments );
    }
    else {
        $.error( 'Method ' + method + ' does not exist on jQuery.sidr' );
    }

};

Upvotes: 1

Views: 1578

Answers (1)

Gideon
Gideon

Reputation: 18491

You should clean up and revert to the state before your plugin was activated.

If you've added elements with bound event handlers, you can simple call remove on them, since this will remove them from the DOM and remove all bound events and data.

Upvotes: 2

Related Questions