X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=blobdiff_plain;f=js%2Fcommunication.js;h=985aeedce59bc4862a09392b5173a38f9855b495;hp=417e8337b4b87186f0aacb36a1f34088bf4e8dff;hb=85dc0facbc2b2de826978fac3768db7949a6b92f;hpb=455f79e2ab07847ea6697e51245288832ae9fede diff --git a/js/communication.js b/js/communication.js index 417e833..985aeed 100644 --- a/js/communication.js +++ b/js/communication.js @@ -19,13 +19,43 @@ // Regroupe la partie communication JSON client -> serveur de euphorik. // 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) { - this.funError = 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) { @@ -35,7 +65,11 @@ euphorik.Communication.prototype.requete = function(action, json, funOk, funErro var mess = this.getBase(action); objectEach(json, function(nom, val) { mess[nom] = val; - }); + }); + + if (this.funDebutReq) { + this.funDebutReq(); + } paramsAjax = { async: asynchrone, @@ -44,7 +78,10 @@ euphorik.Communication.prototype.requete = function(action, json, funOk, funErro dataType: "json", data: { action : JSON.stringify(mess) }, success: - function(data) { + function(data) { + if (thisCommunication.funFinReq) { + thisCommunication.funFinReq(); + } if (data.reply === "error") { if (funError) { funError(data); @@ -54,9 +91,15 @@ 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;