Skip to content

Instantly share code, notes, and snippets.

@nicholasstephan
Last active October 10, 2015 14:17
Show Gist options
  • Save nicholasstephan/3703081 to your computer and use it in GitHub Desktop.
Save nicholasstephan/3703081 to your computer and use it in GitHub Desktop.
JS: jquery plugin
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
}(function ($) {
"use strict";
/* DEFINITION */
var YourPlugin = function() {
};
YourPlugin.prototype = {
};
/* jQuery PLUGIN */
$.fn.yourPlugin = function (option) {
return this.each(function () {
var $this = $(this),
data = $this.data('yourplugin'),
options = $.extend({}, $.fn.yourPlugin.defaults, typeof option == 'object' && option);
if(!data)
$this.data('yourplugin', (data = new YourPlugin(this, options)));
if(typeof option == 'string')
data[option]();
});
};
$.fn.yourPlugin.defaults = {
};
$.fn.yourPlugin.Constructor = YourPlugin;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment