From 9e3c10d4e12e896107c8551b4e6fc1dfbaf7bda1 Mon Sep 17 00:00:00 2001 From: Valerio Virgillito Date: Thu, 16 Feb 2012 21:42:18 -0800 Subject: Support for data attributes and adding a random string generator Signed-off-by: Valerio Virgillito --- js/lib/NJUtils.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/js/lib/NJUtils.js b/js/lib/NJUtils.js index 8daafa42..887743c5 100755 --- a/js/lib/NJUtils.js +++ b/js/lib/NJUtils.js @@ -5,6 +5,7 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot */ var Montage = require("montage/core/core").Montage, + Uuid = require("montage/core/uuid").Uuid, ElementModel = require("js/models/element-model").ElementModel, Properties3D = require("js/models/properties-3d").Properties3D, ShapeModel = require("js/models/shape-model").ShapeModel, @@ -56,13 +57,18 @@ exports.NJUtils = Object.create(Object.prototype, { ///// Quick "createElement" function "attr" can be classname or object ///// with attribute key/values + ///// Suppor for data attributes make : { value: function(tag, attr) { var el = document.createElement(tag); if (typeof attr === 'object') { for (var a in attr) { if (attr.hasOwnProperty(a)) { - el[a] = attr[a]; + if(a.indexOf("data-") > -1) { + el.setAttribute(a, attr[a]); + } else { + el[a] = attr[a]; + } } } } else if (typeof attr === 'string') { @@ -247,6 +253,20 @@ exports.NJUtils = Object.create(Object.prototype, { } return status; } + }, + + /* ================= misc methods ================= */ + + // Generates an alpha-numeric random number + // len: number of chars + // default length is '8' + generateRandom: { + value: function(len) { + var length; + len ? length = len : length = 8; + + return Uuid.generate().substring(0,length); + } } }); -- cgit v1.2.3