Skip to content

Instantly share code, notes, and snippets.

@karlseguin
Created May 16, 2010 17:16
Show Gist options
  • Save karlseguin/403012 to your computer and use it in GitHub Desktop.
Save karlseguin/403012 to your computer and use it in GitHub Desktop.
(function($)
{
$.fn.myPlugin = function(optionsOrCommand, commandOptions)
{
if (optionsOrCommand == 'someCommand')
{
return this.each(function()
{
this.myPlugin.someOtherFunction();
});
}
var opts = $.extend($.fn.myPlugin.defaults, optionsOrCommand);
return this.each(function()
{
if (this.myPlugin) { return false; }
var $this = $(this);
var c =
{
initialize: function()
{
c.someOtherFunction();
},
someOtherFunction: function()
{
}
}
this.myPlugin = c;
c.initialize();
});
};
})(jQuery);
$.fn.myPlugin.defaults =
{
someValue: 'aDefault',
};
/*
by default you initialize it with a straightforward call:
$('something').myPlugin({settings});
but you can also call specific actions after initialization via commands:
$('something').myPlugin('someCommand');
or pass options to those commands:
$('something').myPlugin('someCommand', {options});
*/
@defeated
Copy link

except the defaults should go inside the closure.

also, a little-known shortcut to (function($){ ... })(jQuery); is:

jQuery(function($){ ... }); // saves 2. whole. characters. ;)

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