Skip to content

Instantly share code, notes, and snippets.

@davidbgk
Last active December 17, 2015 11:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidbgk/5603773 to your computer and use it in GitHub Desktop.
Save davidbgk/5603773 to your computer and use it in GitHub Desktop.
Boutez les ignominieuses fonctions anonymes hors de vos jQuery lors de SudWeb 2013
"use strict";
(function($, undefined) {
var Plugin, defaults, namespace;
namespace = 'myPopin';
defaults = {
duration: 1000,
onOpen: function() {}
};
Plugin = (function() {
function Plugin(element, options) {
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = namespace;
this.init();
}
Plugin.prototype.init = function() {};
Plugin.prototype.open = function() {
this.options.onOpen.call(this.element);
};
return Plugin;
})();
$.fn[namespace] = function(options) {
var args, argument;
argument = arguments[0];
args = [].slice.call(arguments, 1);
return this.each(function() {
var plugin;
var pluginName = "plugin_" + namespace;
// Avoid multiple instanciations of the same plugin
// for a given node
plugin = $.data(this, pluginName);
if(!plugin) {
$.data(this, pluginName, new Plugin(this, options));
} else if(plugin[argument] != null &&
$.type(plugin[argument]) == 'function') {
// Using != and not !== to test both null and undefined
return plugin[argument].apply(plugin, args);
}
});
};
return $.fn[namespace];
})(jQuery);
// Example of usages
$('#foo').myPopin();
$('#foo').myPopin('open', 300);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment