Return to Snippet

Revision: 63815
at June 8, 2013 01:36 by rickygri


Updated Code
/* Plugin code */
(function($){
    $.pluginName = function(el, option, callback){
        
        /* Setup base elem vars */
        var base = this;
        base.$el = $(el);
        base.el = el;
        base.$el.data("pluginName", base);
        /* Option object */
        base.o = option;
        
        /*
         * Init
         */
        base.init = function () {
            
        };
        
        /*
         * Callback func when successful (returns element)
         */
        base.success = function() {
            callback(base.$el); // Return elem
        };
        
        base.init();
    };
    $.fn.pluginName = function(option, callback){
        return this.each(function(){
            (new $.pluginName(this, option, callback));
        });
    };
})(jQuery);

/* Use plugin */
$(document).ready(function () {
    
    $(document).pluginName(100, function(elem) {
        // Success
        console.log("Success");
    });
    
});

Revision: 63814
at June 8, 2013 01:35 by rickygri


Initial Code
(function($){
    $.pluginName = function(el, option, callback){
        
        /* Setup base elem vars */
        var base = this;
        base.$el = $(el);
        base.el = el;
        base.$el.data("pluginName", base);
        /* Option object */
        base.o = option;
        
        /*
         * Init
         */
        base.init = function () {
            
        };
        
        /*
         * Callback func when successful (returns element)
         */
        base.success = function() {
            callback(base.$el); // Return elem
        };
        
        base.init();
    };
    $.fn.pluginName = function(option, callback){
        return this.each(function(){
            (new $.pluginName(this, option, callback));
        });
    };
})(jQuery);

$(document).ready(function () {
    
    $(document).pluginName(100, function(elem) {
        // Success
        console.log("Success");
    });
    
});

Initial URL

                                

Initial Description
This is a really basic skeleton template for jQuery plugins. It provides a callback function which can be called anywhere in your plugin with base.success(); This will return the current element, but you can pass anything you like back by changing the base.$el value passed to the function to whatever you like.

Initial Title
Really basic jquery plugin template - with callback

Initial Tags
javascript, jquery

Initial Language
JavaScript