Skip to content

Instantly share code, notes, and snippets.

@mparke
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mparke/9347878 to your computer and use it in GitHub Desktop.
Save mparke/9347878 to your computer and use it in GitHub Desktop.
(function ($, Namespace) {
'use strict';
var slice = Array.prototype.slice;
function widget (widgetName, constructor) {
var namespace = 'fifty.' + widgetName;
return function (options) {
var $el = $(this),
args = slice.call(arguments),
// get the current instance or create a new one
widget = $el.data(namespace) || $el.data(namespace, constructor.apply(this, args)),
methodReturn;
// execute methods and return the method return or this element for chaining
if (typeof options == 'string') {
methodReturn = widget[options].apply(this, args.slice(1, args.length - 1)); // don't include method name
return methodReturn ? methodReturn : $el;
}
// return the widget instance by default
return widget;
};
}
Namespace.widget = widget;
})(jQuery, Namespace);
(function ($, Namespace) {
function Module (options) {
// keep reference to element in closure to be used within module
var $el = $(this);
function method () {}
// public methods
return {
method: method
};
}
$.fn.module = Namepace.widget('module', Module);
})(jQuery, Namespace);
// initializing
var $myPlugin = $('#el').module({});
// I can call the module methods like this
$myPlugin.module().method();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment