Skip to content

Instantly share code, notes, and snippets.

@pixelhandler
Created July 14, 2011 04:08
Show Gist options
  • Save pixelhandler/1081919 to your computer and use it in GitHub Desktop.
Save pixelhandler/1081919 to your computer and use it in GitHub Desktop.
Boilerplate code for jQuery plugin with debug and logging methods
//jQuery.noConflict();
(function($) { // $ is jQuery
// plugin for yada yada
$.fn.yadayada = function(options) {
var defaults = {
foo : 'bar'
};
// Extend our default options with those provided.
var opts = $.extend({}, defaults, options);
// Do something to each item
return this.each(function() {
var _ = { obj : $(this) };
// get settings from options
_.foo = _.obj.find(opts.foo);
/*
... code to return, chained
*/
});
};
// debugging methods
$.fn.debug = function() {
return this.each(function(){
alert(this);
});
};
$.log = function(message) {
if(window.console) {
console.debug(message);
} else {
alert(message);
}
};
// end plugin
// Stuff to do as soon as the DOM is ready;
$(function() {
var something = $('div.something');
try {
if (something.length>0) {
something.yadayada({ foo: 'no bars' });
}
} catch(oops) { $.log(oops); }
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment