-
-
Save thomasdavis/1199466 to your computer and use it in GitHub Desktop.
Universal JavaScript Module, supports AMD (RequireJS), Node.js, and the browser.
This file contains 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 (name, definition){ | |
var theModule = definition(), | |
hasDefine = typeof define === 'function', | |
hasExports = typeof module !== 'undefined' && module.exports; | |
if (hasDefine){ // AMD Module | |
define(theModule); | |
} else if (hasExports) { // Node.js Module | |
module.exports = theModule; | |
} else { // Assign to common namespaces or simply the global object (window) | |
// something like this to allow for flat-file/global module extensions | |
var obj = null; | |
var namespaces = name.split("."); | |
var scope = (this.jQuery || this.ender || this.$ || this); | |
for( var i = 0; i<namespaces.length;i++){ | |
var packageName = namespaces[i]; | |
if( obj && i == namespaces.length-1) { | |
obj[packageName] = theModule; | |
} else if( typeof scope[packageName] === "undefined" ){ | |
scope[packageName] = {}; | |
} | |
obj = scope[packageName]; | |
} | |
} | |
})('core.plugin', function () { | |
// define your module here and return the public API | |
return { author: "addy" }; | |
}); |
This file contains 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
// AMD | |
require(['path/to/myModule'], function (myModule){ | |
// use myModule here | |
}); | |
// Node.js | |
var myModule = require('myModule'); | |
// Common global namespaces | |
jQuery.myModule | |
ender.myModule | |
$.myModule | |
// window | |
myModule |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment