X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=js%2Fcommunication.js;h=3f44227052195aaf5a875111edca104df3f0f74a;hb=79f0cfc91b7220d98e4caf50fbb3857807fc6bc4;hp=417e8337b4b87186f0aacb36a1f34088bf4e8dff;hpb=dd3320de291341d6d86f79421b85d12c4764b057;p=euphorik.git diff --git a/js/communication.js b/js/communication.js index 417e833..3f44227 100644 --- a/js/communication.js +++ b/js/communication.js @@ -20,12 +20,42 @@ // Voir : http://dev.euphorik.ch/wiki/euk/Protocole /** - * @param funError un fonction executé lors d'un réponse 'error' de la part du serveur, peut être redéfinit pour une requête. + * Les fonctions debutReq et finReq servent, par exemple, à afficher à l'utilisateur + * qu'une communication est en cours. + * @param funError un fonction executée lors d'un réponse 'error' de la part du serveur, peut être redéfinit pour une requête. + * @param funDebutReq fonction appelée au début d'une requête (facultatif) + * @param funFinReq fonction appelée à la fin d'une requête (facultatif) */ -euphorik.Communication = function(funError) { +euphorik.Communication = function(funError, funDebutReq, funFinReq) { this.funError = funError; + this.funDebutReq = funDebutReq; + this.funFinReq = funFinReq; }; +/** + * Charge un fichier depuis une url et retourne son contenu. + */ +euphorik.Communication.prototype.load = function(url) { + if (this.funDebutReq) { + this.funDebutReq(); + } + var contenu = ""; + $.ajax({async: false, url: url, success : function(page) { contenu += page; }}); + if (this.funFinReq) { + this.funFinReq(); + } + return contenu; +}; + +/** + * Effectue une requête JSON auprès du serveur. + * @param action une chaine spécifiant l'action, par exemple "put_message" + * @param json les données à envoyer associé à l'action, par exemple {"cookie" : "LKJDLAKSJBFLKASN", "nick" : "Paul", "content" : "Bonjour", "answer_to" : [] } + * @param funOk la fonction exécuté après réception des données du serveur + * @param funError la fonction exécuté si une erreur arrive (facultatif) + * @param asynchrone true pour une communication asychrone (facultatif, truepar défaut) + * @param paramsSupp un objet contenant des paramètres supplémentaire pour la fonction ajax de jQuery (facultatif) + */ euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone, paramsSupp) { var thisCommunication = this; if (asynchrone === undefined) { @@ -36,7 +66,11 @@ euphorik.Communication.prototype.requete = function(action, json, funOk, funErro objectEach(json, function(nom, val) { mess[nom] = val; }); - + + if (this.funDebutReq) { + this.funDebutReq(); + } + paramsAjax = { async: asynchrone, type: "POST", @@ -45,6 +79,9 @@ euphorik.Communication.prototype.requete = function(action, json, funOk, funErro data: { action : JSON.stringify(mess) }, success: function(data) { + if (thisCommunication.funFinReq) { + thisCommunication.funFinReq(); + } if (data.reply === "error") { if (funError) { funError(data); @@ -54,15 +91,21 @@ euphorik.Communication.prototype.requete = function(action, json, funOk, funErro } else if (funOk) { funOk(data); } + }, + error: + function(data) { + if (thisCommunication.funFinReq) { + thisCommunication.funFinReq(); + } } }; - + if (paramsSupp) { objectEach(paramsSupp, function(nom, val) { paramsAjax[nom] = val; }); } - + jQuery.ajax(paramsAjax); }; @@ -70,4 +113,4 @@ euphorik.Communication.prototype.getBase = function(action) { return { "header" : { "action" : action, "version" : euphorik.conf.versionProtocole } }; -}; \ No newline at end of file +};