Skip to content

Instantly share code, notes, and snippets.

@Shavindra
Created December 16, 2015 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shavindra/762861cecce391d08d40 to your computer and use it in GitHub Desktop.
Save Shavindra/762861cecce391d08d40 to your computer and use it in GitHub Desktop.
//uitls.js : CREATE NAMESPACING functions also use ‘define()’ to declare it as requirjs module
(function (requirejs) {
define('utils', function () {
// Allow creation of methods for SHOP modules
window.UTILS = window.UTILS || {};
var u = window.UTILS;
// http://www.kenneth-truyers.net/2013/04/27/javascript-namespaces-and-modules/
u.createNS = function (namespaces) {
for (var i = 0; i < namespaces.length; i++) {
var nsparts = namespaces[i].split(".");
var parent = u;
// we want to be able to include or exclude the root namespace so we strip
// it if it's in the namespace
if (nsparts[0] === "UTILS" || nsparts[0] === "U" || nsparts === "U" || nsparts[0] === "u" || nsparts === "u") {
nsparts = nsparts.slice(1);
}
// loop through the parts and create a nested namespace if necessary
for (var i = 0; i < nsparts.length; i++) {
var partname = nsparts[i];
// check if the current parent already has the namespace declared
// if it isn't, then create it
if (typeof parent[partname] === "undefined") {
parent[partname] = {};
}
// get a reference to the deepest element in the hierarchy so far
parent = parent[partname];
}
// the parent is now constructed with empty namespaces and can be used.
// we return the outermost namespace
return parent;
};
};
//Create a new namespace. This can be in a different file and then injected in to the common.js file
u.createNS(['Data.currencyCodes', 'Data.getCurrencyInfo', 'Data.mapCurrencyCodes']);
u.createNS(['timeStamp'])
u.timeStamp = function () {
var time = new Date();
d = time.getDate() + '-' + time.getMonth() + 1 + '-' + time.getFullYear() + ' ' + time.getHours() + ':' + time.getMinutes(); +':' + time.getSeconds();
return d;
}
return u;
});
})(requirejs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment