From: Florent Bruneau Date: Fri, 25 Feb 2011 19:38:47 +0000 (+0100) Subject: Add jQuery.xapi(call, payload, successcb, errorcb) to call the API from X-Git-Tag: core/1.1.3~13 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=b291bc172a649420710073c774de6b5470376d05;p=platal.git Add jQuery.xapi(call, payload, successcb, errorcb) to call the API from javascript. Signed-off-by: Florent Bruneau --- diff --git a/htdocs/javascript/core.js b/htdocs/javascript/core.js index ea6ccf7..eb0893b 100644 --- a/htdocs/javascript/core.js +++ b/htdocs/javascript/core.js @@ -38,9 +38,29 @@ } }; + 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) { @@ -80,27 +100,27 @@ 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) {