Created
June 1, 2011 18:18
-
-
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
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
/* | |
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