--- a/src/main/resources/scripts/Utils.js Thu Aug 25 17:57:34 2011 -0400
+++ b/src/main/resources/scripts/Utils.js Fri Aug 26 01:11:13 2011 -0400
@@ -10,14 +10,6 @@
};
var Util = {
-
- // get the name of a property as a string, based on its value
- getPropertyString : function(obj, propertyValue) {
- for (key in obj) {
- if (obj[key] === propertyValue) return key;
- }
- return "";
- },
// Web Storage handling
@@ -76,39 +68,6 @@
return ((hours > 0)? Util.formatNumber(hours) + ":": "") + Util.formatNumber(minutes) + ":" + Util.formatNumber(seconds);
},
-
- // send information to the server as JSON
-
- postNewResourceAsJSON : function (uri, data, successHandler) {
- var xhr = new XMLHttpRequest;
-
- try {
- xhr.open("POST", uri, true);
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4) {
- if (xhr.status != 201) {
- Util.log("xhr failed? [" + xhr.status + "] " + xhr.statusText);
- } else {
- var resource = xhr.getResponseHeader("Location");
- Util.log("resource created at " + resource);
- if (successHandler != null) successHandler(resource);
- }
- } else {
- Util.log("xhr readyState is " + xhr.readyState);
- }
- };
- xhr.setRequestHeader("Content-Type", "application/json");
- xhr.setRequestHeader("Content-Length", data.length);
- xhr.setRequestHeader("Accept", "text/plain");
-
- xhr.send(JSON.stringify(data));
-
- } catch (e) {
- Util.log("Failed to send to server " + e);
- return false;
- }
- return true;
- },
// Geolocation related functions
@@ -159,6 +118,51 @@
}
},
+ // send information to the server as JSON
+ // uri is a URI
+ // resource is { contentType : string, data : string }
+ // successHandler is function(XMLHttpRequest)
+ // errorHandler is function(XMLHttpRequest || Exception)
+ post : function (uri, resource, successHandler, errorHandler) {
+
+ var xhr = new XMLHttpRequest;
+ var _emptyFunction = function() { };
+ var successHandler = (!!successHandler)? successHandler : _emptyFunction;
+ var errorHandler = (!!errorHandler)? errorHandler : _emptyFunction;
+ try {
+ xhr.open("POST", uri, true);
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState == 4) {
+ if (xhr.status === 201 || xhr.status === 200) {
+ Util.log("XHR HTTP POST successfull " + xhr.status);
+ successHandler(xhr);
+ } else {
+ Util.log("[ERROR] XHR HTTP POST failed " + xhr.status);
+ errorHandler(xhr);
+ }
+ } else {
+ Util.log("xhr readyState is " + xhr.readyState);
+ }
+ };
+ xhr.setRequestHeader("Content-Type", (!!resource.contentType)? resource.contentType : "application/octet-stream");
+ xhr.setRequestHeader("Content-Length", resource.data.length);
+ xhr.setRequestHeader("Accept", "text/plain");
+ xhr.send(resource.data);
+ } catch (e) {
+ Util.log("Failed to send to server " + e);
+ errorHandler(e);
+ }
+ },
+
+ // send information to the server as JSON
+ postAsJSON : function (uri, data, successHandler, errorHandler) {
+ try {
+ Util.post(uri, { contentType : "application/json", data : JSON.stringify(data) }, successHandler, errorHandler);
+ } catch (e) {
+ errorHandler(e);
+ }
+ },
+
request : function (uri, body, callback, async) {
body = body ? body : null;
callback = callback ? callback : null;