assets/js/romo/base.js in romo-0.19.8 vs assets/js/romo/base.js in romo-0.19.9

- old
+ new

@@ -103,10 +103,18 @@ elem.setAttribute(attrName, attrValue); } return attrValue; } +Romo.prototype.data = function(elem, dataName) { + return this._deserializeValue(this.attr(elem, "data-"+dataName)); +} + +Romo.prototype.setData = function(elem, dataName, dataValue) { + return this.setAttr(elem, "data-"+dataName, String(dataValue)); +} + Romo.prototype.style = function(elem, styleName) { return elem.style[styleName]; } Romo.prototype.setStyle = function(elem, styleName, styleValue) { @@ -431,12 +439,23 @@ ]; // AJAX Romo.prototype.ajax = function(settings) { + var httpMethod = (settings.type || 'GET').toUpperCase(); + var xhrUrl = settings.url || window.location.toString(); + var xhrData = settings.data ? settings.data : null + if (xhrData && httpMethod === 'GET') { + var xhrQuery = Romo.param(xhrData); + if (xhrQuery !== '') { + xhrUrl = (xhrUrl + '&' + xhrQuery).replace(/[&?]{1,2}/, '?'); + } + xhrData = null; + } + var xhr = new XMLHttpRequest(); - xhr.open(settings.type || 'GET', settings.url, true, settings.username, settings.password); + xhr.open(httpMethod, xhrUrl, true, settings.username, settings.password); if (settings.responseType === 'arraybuffer' || settings.responseType === 'blob') { xhr.responseType = settings.responseType; } xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); for (name in settings.headers) { @@ -456,11 +475,11 @@ } else { settings.error.call(window, xhr.statusText || null, xhr.status, xhr, settings); } } }; - xhr.send(settings.data ? settings.data : null); + xhr.send(xhrData); }, // events Romo.prototype.on = function(elem, eventName, fn) { @@ -597,9 +616,23 @@ Romo.prototype._handlers = {}; Romo.prototype._handlerKey = function(elem, eventName, fn) { return 'eid--'+this._el(elem)._romoeid+'--'+eventName+'--fid--'+this._fn(fn)._romofid; +} + +Romo.prototype._deserializeValue = function(value) { + try { + if (value === "true") { return true; } // "true" => true + if (value === "false") { return false; } // "false" => false + if (value === "undefined") { return undefined; } // "undefined" => undefined + if (value === "null") { return null; } // "null" => null + if (+value+"" === value) { return +value; } // "42.5" => 42.5 + if (/^[\[\{]/.test(value)) { JSON.parse(value); } // JSON => parse if valid + return value; // String => self + } catch(e) { + return value + } } // TODO: rework w/o jQuery Romo.prototype._addEventCallback = function(name, callback) { this._eventCallbacks.push({ eventName: name, callback: callback });