Created
July 14, 2011 04:08
-
-
Save pixelhandler/1081919 to your computer and use it in GitHub Desktop.
Boilerplate code for jQuery plugin with debug and logging methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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