Skip to content

Instantly share code, notes, and snippets.

@Fordi
Created August 12, 2011 21:28
Show Gist options
  • Save Fordi/1143063 to your computer and use it in GitHub Desktop.
Save Fordi/1143063 to your computer and use it in GitHub Desktop.
Module template
/**
* This code block is designed to enable the consumer of a script to name your library whatever they want, by simply
* adding the attribute 'data-library-name' to the script tag. When the time comes in your script
* you simply set Module.Space[Module.Name] = Your Cool Library
*
* Other benefits to the Module object*:
* Module.Tag is the script tag that pulled your script in. You can use this to pull in further data-* attributes for script configuration
* Module.URI contains the URI used to get your script. This can be useful if you want to use script-relative resources, such as SWF files
* You can require jQuery just by setting the jQueryVersion argument.
*
* * i.e., your script is being included during page load or has been appended to the page and is, therefore, the last script visible on the DOM in any case.
**/
(function (Module) {
//Your code goes here.
})((function (defaultName, jQueryVersion) {
if (jQueryVersion && (!this.jQuery || (jQuery.fn.jquery < jQueryVersion))) throw new Error(defaultName+' is dependent on jQuery '+jQueryVersion+'+; please load jQuery before this script.');
var libTag = Array.prototype.slice.apply(document.getElementsByTagName('script')).pop(),
Name = libTag.getAttribute('data-library-name') || defaultName,
libName = Name.split('.'),
Parent = this,
name;
while (libName.length>1 && (name = libName.shift()))
Parent = Parent[name] = Parent[name] || {};
return {
FullName: Name,
Space: Parent,
Name: libName.shift(),
Tag: jQuery?jQuery(libTag):libTag,
URI: libTag.getAttribute('src'),
requiresjQuery: jQueryVersion
}
})(/*defaultModuleName, requiredjQueryVersion*/));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment