jQuery Plugin Example
Example of an object-oriented jQuery plugin that allows to initialize using set of options, call functions of the plugin, and have default values for the options
by Patrick Hund
HTML
<div id="columbus">columbus</div>
JavaScript
var Columbus = function (options) {
this.init(options);
};
Columbus.prototype = {
constructor: Columbus,
getOptions: function (options) {
options = $.extend({}, $.fn.columbus.defaults, options);
return options;
},
init: function (options) {
this.options = this.getOptions(options);
},
printPaula: function () {
console.log(this.options.paula);
}
};
$.fn.columbus = function (option) {
return this.each(function () {
var $this = $(this),
data = $this.data('columbus'),
options = typeof option == 'object' && option;
if (!data) {
$this.data('columbus', (data = new Columbus(options)));
}
if (typeof option == 'string') {
data[option]();
}
});
};
$.fn.columbus.defaults = {
paula: 'not so bright'
}
$(document).ready(function () {
/*
$('#columbus').columbus();
*/
$('#columbus').columbus({
paula: 'brillant'
});
$('#columbus').columbus('printPaula');
});