loadJSON -> loadRemote, correctly handle json tls-sprint
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 29 Apr 2009 19:54:50 +0200
branchtls-sprint
changeset 1563 b130c6cec8c2
parent 1562 e6d2c07c0c58
child 1564 de0548cb0ab8
loadJSON -> loadRemote, correctly handle json
web/data/cubicweb.ajax.js
web/data/cubicweb.compat.js
--- a/web/data/cubicweb.ajax.js	Wed Apr 29 19:52:56 2009 +0200
+++ b/web/data/cubicweb.ajax.js	Wed Apr 29 19:54:50 2009 +0200
@@ -29,7 +29,7 @@
  */
 function loadAjaxHtmlHead(node) {
     var head = jQuery('head');
-    var node = jQuery(node).find('div.ajaxHtmlHead');
+    node = jQuery(node).find('div.ajaxHtmlHead');
     _loadAjaxHtmlHead(node, head, 'script', 'src');
     _loadAjaxHtmlHead(node, head, 'link', 'href');
     node.find('*').appendTo(head);
@@ -144,18 +144,6 @@
     }
 }
 
-/*
- * This function is the equivalent of MochiKit's loadJSONDoc but
- * uses POST instead of GET
- */
-function loadJSONDocUsingPOST(url, data) {
-    setProgressCursor();
-    var deferred = loadJSON(url, data, 'POST');
-    deferred = deferred.addErrback(remoteCallFailed);
-    deferred = deferred.addCallback(resetCursor);
-    return deferred;
-}
-
 
 /*
  * This function will call **synchronously** a remote method on the cubicweb server
@@ -187,10 +175,16 @@
  *
  * It looks at http headers to guess the response type.
  */
+
 function asyncRemoteExec(fname /* ... */) {
+    setProgressCursor();
     var props = {'fname' : fname, 'pageid' : pageid,
      		 'arg': map(jQuery.toJSON, sliceList(arguments, 1))};
-    return loadJSONDocUsingPOST(JSON_BASE_URL, props);
+    var deferred = loadRemote(JSON_BASE_URL, props, 'POST');
+    deferred = deferred.addErrback(remoteCallFailed);
+    deferred = deferred.addErrback(resetCursor);
+    deferred = deferred.addCallback(resetCursor);
+    return deferred;
 }
 
 
--- a/web/data/cubicweb.compat.js	Wed Apr 29 19:52:56 2009 +0200
+++ b/web/data/cubicweb.compat.js	Wed Apr 29 19:54:50 2009 +0200
@@ -365,11 +365,16 @@
 };
 
 
-function loadJSON(url, data, type) {
+/*
+ * Asynchronously load an url and return a deferred
+ * whose callbacks args are decoded according to
+ * the Content-Type response header
+ */
+function loadRemote(url, data, reqtype) {
     var d = new Deferred();
     jQuery.ajax({
 	url: url,
-	type: type,
+	type: reqtype,
 	data: data,
 
 	beforeSend: function(xhr) {
@@ -377,6 +382,9 @@
 	},
 
 	success: function(data, status) {
+            if (d.req.getResponseHeader("content-type") == 'application/json') {
+              data = evalJSON(data);
+            }
 	    d.success(data);
 	},
 
@@ -506,23 +514,7 @@
 var KEYS = {
     KEY_ESC: 27,
     KEY_ENTER: 13
-}
+};
 
-// XHR = null;
-// function test() {
-//     var d = loadJSON('http://crater:9876/json?mode=remote&fname=i18n&pageid=xxx&arg=' + jQuery.toJSON(['modify']));
-//     d = d.addCallback(function (result, xhr) {
-// 	XHR = xhr;
-// 	log('got ajax result 1' + result + xhr);
-// 	log('got ajax result 1' + xhr);
-// 	log('got ajax result 1' + xhr + 'arguments =', arguments.length);
-//     });
-//     d.addCallback(function (x, req, y, z) {
-// 	log('callback 2 x =' + x, ' req=', req, 'y =', y, 'z=',z);
-//     }, 12, 13)
-//     d.addErrback(function (error, xhr) {
-// 	XHR = xhr;
-// 	log('got err', error, ' code =', xhr.status, 'arguments length=', arguments.length);
-//     })
-// }
 
+