Widget Factory

by abernier

HTML

<h1 id="foo">Fooooooo</h1>

JavaScript

(function($) {
    function redman(node) {
        var $node = $(node);
        
        function init(options) {
            $node.addClass("wu-tang").css("color","red");
            
            return this;
        }
        
        function destroy() {
            $node.removeClass("wu-tang").removeAttr("style");
        }
        
        return {
            init: init,
            destroy: destroy
        };
    }
    
    $.fn.redman = function (options) {
        return this.each(function () {
            $.data(this, "redman", redman(this).init(options));
        });
    };
}(jQuery));           
 

var foo = $('#foo').redman();
console.log(foo);
setTimeout(function() {
    foo.data("redman").destroy();
}, 2000);