X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=js%2Fcommunication.js;h=3f44227052195aaf5a875111edca104df3f0f74a;hb=79f0cfc91b7220d98e4caf50fbb3857807fc6bc4;hp=985aeedce59bc4862a09392b5173a38f9855b495;hpb=5b07219194951e5ca4c1f4a7f3ca246333dd93a1;p=euphorik.git diff --git a/js/communication.js b/js/communication.js index 985aeed..3f44227 100644 --- a/js/communication.js +++ b/js/communication.js @@ -19,42 +19,42 @@ // Regroupe la partie communication JSON client -> serveur de euphorik. // Voir : http://dev.euphorik.ch/wiki/euk/Protocole -/** - * Les fonctions debutReq et finReq servent, par exemple, à afficher à l'utilisateur +/** + * 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 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, funDebutReq, funFinReq) { - this.funError = funError; - this.funDebutReq = funDebutReq; + 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) + +/** + * 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; @@ -65,12 +65,12 @@ 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(); + }); + + if (this.funDebutReq) { + this.funDebutReq(); } - + paramsAjax = { async: asynchrone, type: "POST", @@ -78,9 +78,9 @@ euphorik.Communication.prototype.requete = function(action, json, funOk, funErro dataType: "json", data: { action : JSON.stringify(mess) }, success: - function(data) { - if (thisCommunication.funFinReq) { - thisCommunication.funFinReq(); + function(data) { + if (thisCommunication.funFinReq) { + thisCommunication.funFinReq(); } if (data.reply === "error") { if (funError) { @@ -91,21 +91,21 @@ euphorik.Communication.prototype.requete = function(action, json, funOk, funErro } else if (funOk) { funOk(data); } - }, - error: - function(data) { - if (thisCommunication.funFinReq) { - thisCommunication.funFinReq(); - } + }, + error: + function(data) { + if (thisCommunication.funFinReq) { + thisCommunication.funFinReq(); + } } }; - + if (paramsSupp) { objectEach(paramsSupp, function(nom, val) { paramsAjax[nom] = val; }); } - + jQuery.ajax(paramsAjax); }; @@ -113,4 +113,4 @@ euphorik.Communication.prototype.getBase = function(action) { return { "header" : { "action" : action, "version" : euphorik.conf.versionProtocole } }; -}; \ No newline at end of file +};