Created
February 21, 2012 23:54
-
-
Save phette23/1879951 to your computer and use it in GitHub Desktop.
The Mike Alsup jQuery Plugin Development Pattern
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
// | |
//taken verbatim from jQuery Fundamentals | |
//which is at http://jqfundamentals.com/ (see chap. 8) | |
//and has an unmaintained github repo at https://github.com/rmurphey/jqfundamentals | |
// | |
// create closure | |
// | |
(function($) { | |
// | |
// plugin definition | |
// | |
$.fn.hilight = function(options) { | |
debug(this); | |
// build main options before element iteration | |
var opts = $.extend({}, $.fn.hilight.defaults, options); | |
// iterate and reformat each matched element | |
return this.each(function() { | |
$this = $(this); | |
// build element specific options | |
var o = $.meta ? $.extend({}, opts, $this.data()) : opts; | |
// update element styles | |
$this.css({ | |
backgroundColor: o.background, | |
color: o.foreground | |
}); | |
var markup = $this.html(); | |
// call our format function | |
markup = $.fn.hilight.format(markup); | |
$this.html(markup); | |
}); | |
}; | |
// | |
// private function for debugging | |
// | |
function debug($obj) { | |
if (window.console && window.console.log) | |
window.console.log('hilight selection count: ' + $obj.size()); | |
}; | |
// | |
// define and expose our format function | |
// | |
$.fn.hilight.format = function(txt) { | |
return '<strong>' + txt + '</strong>'; | |
}; | |
// | |
// plugin defaults | |
// | |
$.fn.hilight.defaults = { | |
foreground: 'red', | |
background: 'yellow' | |
}; | |
// | |
// end of closure | |
// | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment