toannk
10/5/2017 - 10:52 AM

jQuery OOP plugin pattern

jQuery OOP plugin pattern

/*
	Initialization:
	$('element').pluginName({
		optionName: 'thatCanOvervriteDefaults'
	});

	Public method:
	$('element').data('pluginName').public();

	Get or set the value of a property:
	$('element').data('pluginName').plugin.o.optionName;
*/

;(function($){
'use strict';
$.pluginName = function (element, options) {
	
	var $element = $(element),
		_this = this,
		_defaultOptions = {
			optionName: true
		},
		_pluginOptions = options,
		_dataOptions = $element.data() || {};
	
	_this.plugin = {};
	_this.plugin.o = $.extend({}, _defaultOptions, _pluginOptions, _dataOptions);

	var init = function() {};
	
	var private = function () {};
	
	_this.public = function () {};
	
	init();
};

$.fn.pluginName = function(options) {
	
	return this.each(function () {
		
		if (undefined === $(this).data('pluginName')) {
			
			var plugin = new $.pluginName(this, options);

			$(this).data('pluginName', plugin);
		}
	});
};
})(jQuery);