Last active
August 29, 2015 13:57
-
-
Save mparke/9347878 to your computer and use it in GitHub Desktop.
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
(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