MOD avancement dans le module 'communication'
[euphorik.git] / js / communication.js
diff --git a/js/communication.js b/js/communication.js
new file mode 100644 (file)
index 0000000..db9beb8
--- /dev/null
@@ -0,0 +1,62 @@
+// coding: utf-8\r
+// Copyright 2008 Grégory Burri\r
+//\r
+// This file is part of Euphorik.\r
+//\r
+// Euphorik is free software: you can redistribute it and/or modify\r
+// it under the terms of the GNU General Public License as published by\r
+// the Free Software Foundation, either version 3 of the License, or\r
+// (at your option) any later version.\r
+//\r
+// Euphorik is distributed in the hope that it will be useful,\r
+// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+// GNU General Public License for more details.\r
+//\r
+// You should have received a copy of the GNU General Public License\r
+// along with Euphorik.  If not, see <http://www.gnu.org/licenses/>.\r
+\r
+// regroupe la partie communication client -> sevreur de euphorik.\r
+\r
+/**\r
+  * @param funError un fonction executé lors d'un réponse 'error' de la part du serveur, peut être redéfinit pour une requête.\r
+  */\r
+euphorik.Communication = function(util, funError) {\r
+}\r
+\r
+euphorik.Communication.prototype.getBase = function(action) {\r
+   this.base = {\r
+      "header" : { "action" : action, "version" : euphorik.conf.versionProtocole }\r
+   };\r
+}\r
+\r
+euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone) {\r
+   if (asynchrone === undefined) {\r
+      asynchrone = true; // asynchrone par défaut\r
+   }\r
+\r
+   var mess = this.getbase(action);\r
+   objectEach(json, function(nom, val) {\r
+      mess[nom] = val;\r
+   });\r
+   \r
+   jQuery.ajax({\r
+      async: asynchrone,\r
+      type: "POST",\r
+      url: "request",\r
+      dataType: "json",\r
+      data: { action : JSON.stringify(mess) }\r
+      success:\r
+         function(data) {\r
+            if (data.reply === "error") {\r
+               if (funError) {\r
+                  funError(data);\r
+               } else if (this.funError) {\r
+                  this.funError(data);\r
+               }               \r
+            } else if (funOk) {\r
+               funOk(data)\r
+            }\r
+         }\r
+   });\r
+}\r