Add jQuery.xapi(call, payload, successcb, errorcb) to call the API from
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Fri, 25 Feb 2011 19:38:47 +0000 (20:38 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Fri, 25 Feb 2011 19:42:58 +0000 (20:42 +0100)
javascript.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
htdocs/javascript/core.js

index ea6ccf7..eb0893b 100644 (file)
         }
     };
 
+    var ajaxParams = function(onSuccess, onError, extraParameters) {
+        return $.extend({
+            success: function(data, textStatus, xhr) {
+                if (textStatus === 'success') {
+                    if (onSuccess) {
+                        onSuccess(data, textStatus, xhr);
+                    }
+                } else if (textStatus === 'error') {
+                    if (onError) {
+                        onError(data, textStatus, xhr);
+                    } else {
+                        alert("Une error s'est produite lors du traitement de la requête.\n"
+                            + "Ta session a peut-être expiré");
+                    }
+                }
+            }
+        }, extraParameters);
+    };
 
     /* Add new functions to jQuery namesapce */
     $.extend({
+        xapiVersion: '1',
+
         plURL: (function() {
             var base;
             return function(url) {
                 onError = null;
             }
 
-            function ajaxHandler(data, textStatus, xhr) {
-                if (textStatus === 'success') {
-                    if (onSuccess) {
-                        onSuccess(data, textStatus, xhr);
-                    }
-                } else if (textStatus === 'error') {
-                    if (onError) {
-                        onError(data, textStatus, xhr);
-                    } else {
-                        alert("Une error s'est produite lors du traitement de la requête.\n"
-                            + "Ta session a peut-être expiré");
-                    }
-                }
-            }
-            return $.ajax({
+            return $.ajax(ajaxParams(onSuccess, onError, {
                 url: $.plURL(source),
                 type: method,
-                success: ajaxHandler,
                 data : data,
                 dataType: type
-            });
+            }));
+        },
+
+        xapi: function(apicall, payload, onSuccess, onError) {
+            if ($.isFunction(payload)) {
+                onError   = onSuccess;
+                onSuccess = payload;
+            }
+
+            return $.ajax(ajaxParams(onSuccess, onError, {
+                url: $.plURL('api/' + $.xapiVersion + '/' + apicall),
+                type: 'POST',
+                data: JSON.stringify(payload),
+                dataType: 'json',
+                contentType: 'text/javascript'
+            }));
         },
 
         xget: function(source, data, onSuccess, onError, type) {