db9beb887fd7d09b4079293376c00ca0ea02d775
[euphorik.git] / js / communication.js
1 // coding: utf-8
2 // Copyright 2008 Grégory Burri
3 //
4 // This file is part of Euphorik.
5 //
6 // Euphorik is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Euphorik is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
18
19 // regroupe la partie communication client -> sevreur de euphorik.
20
21 /**
22 * @param funError un fonction executé lors d'un réponse 'error' de la part du serveur, peut être redéfinit pour une requête.
23 */
24 euphorik.Communication = function(util, funError) {
25 }
26
27 euphorik.Communication.prototype.getBase = function(action) {
28 this.base = {
29 "header" : { "action" : action, "version" : euphorik.conf.versionProtocole }
30 };
31 }
32
33 euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone) {
34 if (asynchrone === undefined) {
35 asynchrone = true; // asynchrone par défaut
36 }
37
38 var mess = this.getbase(action);
39 objectEach(json, function(nom, val) {
40 mess[nom] = val;
41 });
42
43 jQuery.ajax({
44 async: asynchrone,
45 type: "POST",
46 url: "request",
47 dataType: "json",
48 data: { action : JSON.stringify(mess) }
49 success:
50 function(data) {
51 if (data.reply === "error") {
52 if (funError) {
53 funError(data);
54 } else if (this.funError) {
55 this.funError(data);
56 }
57 } else if (funOk) {
58 funOk(data)
59 }
60 }
61 });
62 }