FIX#64
[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 JSON client -> serveur de euphorik.
20 // Voir : http://dev.euphorik.ch/wiki/euk/Protocole
21
22 /**
23 * @param funError un fonction executé lors d'un réponse 'error' de la part du serveur, peut être redéfinit pour une requête.
24 */
25 euphorik.Communication = function(funError) {
26 this.funError = funError;
27 };
28
29 euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone, paramsSupp) {
30 var thisCommunication = this;
31 if (asynchrone === undefined) {
32 asynchrone = true; // asynchrone par défaut
33 }
34
35 var mess = this.getBase(action);
36 objectEach(json, function(nom, val) {
37 mess[nom] = val;
38 });
39
40 paramsAjax = {
41 async: asynchrone,
42 type: "POST",
43 url: "request",
44 dataType: "json",
45 data: { action : JSON.stringify(mess) },
46 success:
47 function(data) {
48 if (data.reply === "error") {
49 if (funError) {
50 funError(data);
51 } else if (thisCommunication.funError) {
52 thisCommunication.funError(data);
53 }
54 } else if (funOk) {
55 funOk(data);
56 }
57 }
58 };
59
60 if (paramsSupp) {
61 objectEach(paramsSupp, function(nom, val) {
62 paramsAjax[nom] = val;
63 });
64 }
65
66 jQuery.ajax(paramsAjax);
67 };
68
69 euphorik.Communication.prototype.getBase = function(action) {
70 return {
71 "header" : { "action" : action, "version" : euphorik.conf.versionProtocole }
72 };
73 };