Last active
December 17, 2015 11:39
-
-
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
This file contains hidden or 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
"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