]> git.parisson.com Git - telemeta.git/commitdiff
add ajax sync method
authorGuillaume Pellerin <yomguy@parisson.com>
Sun, 20 Jul 2014 17:11:43 +0000 (19:11 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Sun, 20 Jul 2014 17:11:43 +0000 (19:11 +0200)
telemeta/static/telemeta/js/application.js

index e5e933e8b745d340375aeabdeda5f857a5cdae83..313ef896c4af2f8291ba9835ea3229fb2e426039 100644 (file)
@@ -185,6 +185,78 @@ var json = function(param,method,onSuccessFcn,onErrorFcn){
     data2send+=method;
     data2send+='","jsonrpc":"1.0"}';
 
+    var $J = jQuery;
+    $J.ajax({
+        type: "POST",
+        url: 'json/',
+        contentType: "application/json",
+        data: data2send,
+        dataType: "json",
+        success: function(data, textStatus, jqXHR){
+            if(onSuccessFcn){
+                onSuccessFcn(data, textStatus, jqXHR);
+            }
+        },
+        error: function(jqXHR, textStatus, errorThrown){
+            if(onErrorFcn){
+                onErrorFcn(jqXHR, textStatus, errorThrown);
+                return;
+            }
+            //default:
+            var details = "\n(no further info available)";
+            if(jqXHR) {
+                details="\nThe server responded witha status of "+jqXHR.status+" ("+
+                jqXHR.statusText+")\n\nDetails (request responseText):\n"+jqXHR.responseText;
+            }
+            alert("ERROR: Failed to save"+details);
+
+        }
+    });
+
+};
+
+
+
+var json_sync = function(param,method,onSuccessFcn,onErrorFcn){
+    //this function converts a javascript object to a string
+    var toString_ = function(string){
+        if(typeof string == "string"){
+            //escapes newlines quotes and backslashes
+            string = string.replace(/\\/g,"\\\\")
+            .replace(/\n/g,"\\n")
+            .replace(/"/g,"\\\"");
+        }
+        var array; //used for arrays and objects (see below)
+        if(typeof string == "boolean" || typeof string== "number" || typeof string == "string"){
+            string = '"'+string+'"';
+        }else if(string instanceof Array){
+            array = [];
+            for(var i = 0;i <string.length ; i++){
+                array.push(toString_(string[i])); //recursive invocation
+            }
+            string='[';
+            string+=array.join(",");
+            string+=']';
+        }else{
+            array = [];
+            for(var k in string){
+                array.push(toString_(k)+":"+toString_(string[k])); //recursive invocation
+            }
+            string='{';
+            string+=array.join(",");
+            string+='}';
+        }
+        return string;
+    };
+
+    //creating the string to send.
+    var param2string = toString_(param);
+    var data2send = '{"id":"jsonrpc", "params":';
+    data2send+=param2string;
+    data2send+=', "method":"'
+    data2send+=method;
+    data2send+='","jsonrpc":"1.0"}';
+
     var $J = jQuery;
     $J.ajax({
         type: "POST",
@@ -216,6 +288,9 @@ var json = function(param,method,onSuccessFcn,onErrorFcn){
 
 };
 
+
+
+
 /**
  * function for writing to the console. Catches errors, if any (eg, console == undefined) and does nothing in case
  */