Skip to content

Instantly share code, notes, and snippets.

@tbreuss
Last active January 30, 2021 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbreuss/ad5d54c5f5655b55ed1b5c6808287084 to your computer and use it in GitHub Desktop.
Save tbreuss/ad5d54c5f5655b55ed1b5c6808287084 to your computer and use it in GitHub Desktop.
jQuery Tips & Tricks
/*!
* jQuery simpleWidget - v0.1pre - 10/28/2010
* http://benalman.com/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function($,undefined){
'$:nomunge'; // Used by YUI compressor.
$.simpleWidget = function( widget_name, widget ) {
// Create selector.
$.expr[':'][ widget_name ] = function( elem ) {
return !!$.data( elem, widget_name );
};
// Create plugin method.
$.fn[ widget_name ] = function( options ) {
var args,
args_to_slice = 0,
method_name = '_create',
result;
// Was a method name passed as the first argument?
if ( typeof options === 'string' && $.isFunction( widget[ options ] ) ) {
method_name = options;
args_to_slice = 1;
}
// Parse the passed arguments.
args = Array.prototype.slice.call( arguments, args_to_slice );
// Call the appropriate method in the context of each passed element's
// instance.
this.each(function(){
var elem = $(this),
data = elem.data( widget_name ),
created = data,
method;
// If widget has no data, initialize instance data.
data || elem.data( widget_name, data = $.extend( {}, widget, {
element: elem,
options: options
}));
// Only execute method when appropriate.
if ( created ? method_name !== '_create' : method_name !== 'destroy' ) {
method = data[ method_name ];
result = $.isFunction( method ) && method.apply( data, args );
}
if ( method_name === 'destroy' ) {
// If destroying a widget, remove instance data.
elem.removeData( widget_name );
} else if ( result !== undefined ) {
// If method returned a non-undefined value, break out of the each.
return false;
}
});
// If method returned a non-undefined value, return that value (this will
// break the chain).
return result !== undefined ? result : this;
};
};
})(jQuery);

How to create jquery plugin with several actions.

(function( $ ){

    var methods = {
        init : function(options) {

        },
        show : function( ) {    },// IS
        hide : function( ) {  },// GOOD
        update : function( content ) {  }// !!!
    };

    $.fn.tooltip = function(methodOrOptions) {
        if ( methods[methodOrOptions] ) {
            return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
            // Default to "init"
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  methodOrOptions + ' does not exist on jQuery.tooltip' );
        }    
    };


})( jQuery );

Taken from https://stackoverflow.com/questions/1117086/how-to-create-a-jquery-plugin-with-methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment