Preparation à l'AJAXisation des communications
This commit is contained in:
parent
9483937902
commit
b145b13dea
|
@ -23,7 +23,8 @@ $answerType = (isset($_GET['type'])) ? $_GET['type'] : null;
|
|||
<link rel='stylesheet' href='css/global.css'/>
|
||||
<link rel='stylesheet' href='css/responsive.css'/>
|
||||
|
||||
<script type='text/javascript' src='js/adjust.js'></script>
|
||||
<script type='text/javascript' src='js/lib/adjust.js'></script>
|
||||
<script type='text/javascript' src='js/lib/API.js'></script>
|
||||
<script type='text/javascript' src='js/input-checker.js'></script>
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
@ -18,7 +18,8 @@ if(!Authentification::checkUser(0)){
|
|||
<link rel='stylesheet' href='css/global.css'/>
|
||||
<link rel='stylesheet' href='css/responsive.css'/>
|
||||
|
||||
<script type='text/javascript' src='js/adjust.js'></script>
|
||||
<script type='text/javascript' src='js/lib/adjust.js'></script>
|
||||
<script type='text/javascript' src='js/lib/API.js'></script>
|
||||
<script type='text/javascript' src='js/input-checker.js'></script>
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
@ -24,7 +24,8 @@ $answerType = (isset($_GET['type'])) ? $_GET['type'] : null;
|
|||
<link rel='stylesheet' href='css/blue-green.theme.css'/>
|
||||
<link rel='stylesheet' href='css/responsive.css'/>
|
||||
|
||||
<script type='text/javascript' src='js/adjust.js'></script>
|
||||
<script type='text/javascript' src='js/lib/adjust.js'></script>
|
||||
<script type='text/javascript' src='js/lib/API.js'></script>
|
||||
<script type='text/javascript' src='js/input-checker.js'></script>
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
@ -24,7 +24,8 @@ $answerType = (isset($_GET['type'])) ? $_GET['type'] : null;
|
|||
<link rel='stylesheet' href='css/purple.theme.css'/>
|
||||
<link rel='stylesheet' href='css/responsive.css'/>
|
||||
|
||||
<script type='text/javascript' src='js/adjust.js'></script>
|
||||
<script type='text/javascript' src='js/lib/adjust.js'></script>
|
||||
<script type='text/javascript' src='js/lib/API.js'></script>
|
||||
<script type='text/javascript' src='js/input-checker.js'></script>
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/* classe API */
|
||||
function APIClass(){};
|
||||
|
||||
APIClass.prototype = {
|
||||
xhr: [], // tableau d'objets pour les requêtes ajax
|
||||
|
||||
|
||||
|
||||
/* transaction avec le serveur (managers)
|
||||
*
|
||||
* @param pRequest<Object> l'objet passé en JSON à API.php
|
||||
* @param pHandler<Function> fonction qui s'éxécutera lors de la réponse (1 argument -> réponse<Object>)
|
||||
*
|
||||
* @return answer<Object> l'objet retourné par API.php via pHandler (1er argument)
|
||||
*
|
||||
***************************************************************************************************
|
||||
*
|
||||
* @usecase
|
||||
* 1. var answerObject = sendRequest(
|
||||
* 2. { var1: "exemple", var2: 198294 },
|
||||
* 3. function(rep){ alert(rep); }
|
||||
* 4. );
|
||||
* @explain
|
||||
* 1. on appelle la fonction <=> on créé la requête
|
||||
* 2. on passe l'objet qui sera envoyé
|
||||
* 3. on passe une fonction qui utilise un argument (sera la réponse de API.php) (sous forme d'objet)
|
||||
*
|
||||
*/
|
||||
send: function(pRequest, pHandler){
|
||||
|
||||
// on efface les requêtes qui sont terminées (toutes celles de this.xhr)
|
||||
for( var i = 0 ; i < this.xhr.length ; i++ ){
|
||||
if( this.xhr[i].readyState == 4 ) // si terminée
|
||||
this.xhr = this.xhr.slice(0,i-1).concat(this.xhr.slice(i,this.xhr.length-1)); // suppression entrée
|
||||
}
|
||||
|
||||
// on créé une nouvelle entrée
|
||||
this.xhr.push(null);
|
||||
i = this.xhr.length-1;
|
||||
|
||||
// création de l'objet AJAX
|
||||
if(window.XMLHttpRequest) // IE7+, Firefox, Chrome, Opera, Safari
|
||||
this.xhr[i] = new XMLHttpRequest();
|
||||
else // IE5, IE6
|
||||
this.xhr[i] = new ActiveXObject('Microsoft.XMLHttpRequest');
|
||||
|
||||
console.log(pRequest);
|
||||
|
||||
var ptrAPI = this;
|
||||
this.xhr[i].onreadystatechange = function(){
|
||||
if( ptrAPI.xhr[i].readyState == 4 ){ // si la requête est terminée
|
||||
|
||||
/* DEBUG : affiche la réponse BRUTE de API.php */
|
||||
console.log('managers/ => '+ptrAPI.xhr[i].responseText);
|
||||
console.log( JSON.parse(ptrAPI.xhr[i].responseText) );
|
||||
|
||||
/* si success de requête */
|
||||
if( [0,200].indexOf(ptrAPI.xhr[i].status) > -1 ){ // si fichier existe et reçu
|
||||
try{ pHandler( JSON.parse(ptrAPI.xhr[i].responseText) ); } // si on peut parser, on envoie
|
||||
catch(e){ pHandler({status:'corrupted'}); } // sinon on envoie obj.status = 'corrupted'
|
||||
}
|
||||
/* sinon retourne obj.status = 'unreachable' */
|
||||
else
|
||||
pHandler({status: 'unreachable'});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// on créé un formulaire POST (virtuel)
|
||||
var form = new FormData();
|
||||
form.append('json', JSON.stringify(pRequest) ); // on créé la variable $_POST['json']=>request
|
||||
|
||||
this.xhr[i].open('POST', 'managers/', true);
|
||||
this.xhr[i].send( form );
|
||||
|
||||
}
|
||||
};
|
|
@ -66,6 +66,9 @@ sbCreer.addEventListener('click', function(e){
|
|||
formElements[i].value = ''; // on vide
|
||||
}}
|
||||
|
||||
if( checker ) // si tout es ok uniquement, on submit()
|
||||
if( checker ){ // si tout es ok uniquement, on submit()
|
||||
sbCreer.parentNode.submit();
|
||||
}else{ // sinon on affiche l'erreur
|
||||
notif('error', 'Oups!', 'Certains champs sont requis ou incorrects.');
|
||||
}
|
||||
}, false);
|
|
@ -50,7 +50,7 @@ class Manager{
|
|||
public function dispatch($params){
|
||||
if(($this->managerFound instanceof Authentification) or (Authentification::checkUser($this->commandFound['role'],$this->commandFound['strict']))){
|
||||
if(method_exists($this->managerFound,$this->commandFound['method'])){
|
||||
$evalCommand = '$this->managerFound->'.$this->commandFound['method'].'($params)?>';
|
||||
$evalCommand = '$this->managerFound->'.$this->commandFound['method'].'($params); ?>';
|
||||
eval($evalCommand);
|
||||
}else{
|
||||
Response::quickResponse(500,json_encode("La méthode: ".$this->commandFound['method'].' n\'est pas présente dans le manager'));
|
||||
|
|
Loading…
Reference in New Issue