Skip to content

Instantly share code, notes, and snippets.

@HenrikJoreteg
Created June 1, 2011 18:18
Show Gist options
  • Save HenrikJoreteg/1002916 to your computer and use it in GitHub Desktop.
Save HenrikJoreteg/1002916 to your computer and use it in GitHub Desktop.
Solution for utility functions to be used with underscore.js as mixins or commonjs
/*
Building a module this way allows you to do this on the client:
<script src="underscore.js"></script>
<script src="myutils.js"></script>
Then use it in your code as children of '_' without polluting your global namespace.
_.myFunction();
And in node.js you can just require the functions themselves like so:
var myUtilFunctions = require('./myutils');
myUtilFuncions.myFuncion();
*/
(function () {
var server = (typeof exports !== 'undefined'),
_ = this._;
if (!_ && (typeof require !== 'undefined')) _ = require("underscore")._;
// attach your utility functions to this object
var pub = {
myFunction: function () {
}
}
if (server) {
module.exports = pub;
} else {
_.mixin(pub);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment