jQuery Plugin Template
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
/** | |
* Author: Tony Brix, Tony@Brix.ninja | |
* License: MIT http://www.opensource.org/licenses/mit-license.php | |
* Template Gist: https://gist.github.com/UziTech/f85d7756ed931c304281 | |
* Description: | |
*/ | |
; | |
(function ($, undefined) { | |
"use strict"; | |
var pluginName = "plugintemplate";//TODO: ENTER YOUR PLUGIN NAME | |
$[pluginName] = function (el, options) { | |
var plugin; | |
if ($(el).data(pluginName)) { | |
plugin = $(el).data(pluginName); | |
} else { | |
// To avoid scope issues, use 'plugin' instead of 'this' | |
// to reference this class from internal events and functions. | |
plugin = this; | |
// Access to jQuery and DOM versions of element | |
plugin.$el = $(el); | |
plugin.el = el; | |
plugin._defaults = { | |
//TODO: PUT DEFAULT OPTIONS HERE | |
}; | |
plugin._init = function () { | |
if (plugin.$el.data(pluginName)) { | |
throw "Already Initialized"; | |
} | |
plugin.options = $.extend({}, plugin._defaults, options); | |
//TODO: PUT INITIALIZATION CODE HERE | |
// Add a reverse reference to the DOM object | |
plugin.$el.data(pluginName, plugin); | |
}; | |
plugin.destroy = function () { | |
//TODO: PUT DESCTRUCTION CODE HERE | |
delete plugin.$el.data()[pluginName]; | |
}; | |
// Sample Function | |
// | |
// Can be accessed with $(...).pluginName("functionName", parameter1, parameter2, ...); | |
// plugin.functionName = function(paramater1, parameter2, ...){ | |
// | |
// }; | |
// | |
// Functions with "_" prefix are private and can only be accessed within this plugin | |
// plugin._functionName = function(parameters){ | |
// | |
// }; | |
// | |
} | |
if (typeof (options) === "object" || options === undefined) { | |
plugin._init(); | |
} else if (typeof (options) === "string" && options.substring(0, 1) !== "_" && typeof plugin[options] === "function") { | |
plugin[options].apply(plugin, Array.prototype.slice.call(arguments, 1)); | |
} else { | |
throw "Invalid Arguments"; | |
} | |
}; | |
$.fn[pluginName] = function (options) { | |
if (typeof options === "string") { | |
var returnVal = this; | |
this.each(function () { | |
var val = $[pluginName](this, options); | |
if (val !== undefined) { | |
returnVal = val; | |
} | |
}); | |
return returnVal; | |
} else { | |
return this.each(function () { | |
(new $[pluginName](this, options)); | |
}); | |
} | |
}; | |
//uncomment to add stylesheet before other style sheets to allow overloading | |
/* | |
$(function () { | |
var $style = $("<style class='" + pluginName + "-stylesheet'>" + | |
"" + | |
"</style>"); | |
var $styles = $("head link[rel='stylesheet'], head style"); | |
if ($styles.length > 0) { | |
$styles.eq(0).before($style); | |
} else { | |
$("head").append($style); | |
} | |
}); | |
*/ | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment