UserSpice
Ajax Engine for UserSpice V0.1 - Printable Version

+- UserSpice (https://userspice.com/forums)
+-- Forum: General (https://userspice.com/forums/forumdisplay.php?fid=20)
+--- Forum: UserSpice 5 - Roadmapping the Future (https://userspice.com/forums/forumdisplay.php?fid=31)
+--- Thread: Ajax Engine for UserSpice V0.1 (/showthread.php?tid=168)



Ajax Engine for UserSpice V0.1 - Ponchale - 06-16-2016

create one file called ajax.js and put the following

<pre>
Code:
Ajax={};

Ajax.buildRequest = function(metodo, url, callback)
{
this.request = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP");
this.request.onreadystatechange = callback;
this.request.open(metodo, url, true);
this.request.send(url);
}

Ajax.checkState = function(_id)
{
switch(this.request.readyState)
{
case 0:
// se aƱade logica para actualizar algun componente visual por ejemplo
document.getElementById(_id).innerHTML = 'Sin inicializar ...';
break;

case 1:
document.getElementById(_id).innerHTML = 'Cargando ...';
break;

case 2:
document.getElementById(_id).innerHTML = 'Cargado...';
break;

case 3:
document.getElementById(_id).innerHTML = 'Interactivo...';
break;

case 4:
document.getElementById(_id).innerHTML = 'Listo';
return this.request.status;
}
}

Ajax.getResponse = function()
{
if(this.request.getResponseHeader('Content-Type').indexOf('xml') != -1)
{
return this.request.responseXML.documentElement;
}
else
{
return this.request.responseText;
}
}
</pre>


after create the file Update.js and put the following

<pre>
Code:
Updater = {};

Updater.initialize = function()
{
    Updater.isWorking = false;
}
Updater.initialize();

Updater.process = function(method , service, callback)
{
    if(callback == undefined || callback == "") { callback = Updater.onResponse; }
    Ajax.buildRequest(method, service, callback);
    Updater.isWorking = true;
}

Updater.onResponse = function()
{
   // Hacemos algo con la peticion ( feel free) ponga aqui su codigo
    if(Ajax.checkState('loading') == 200)
    {
        Updater.isWorking = false;
    }
}
</pre>


I hope this little Ajax engine is integrated with UserSpace and subsequently continue improving




Ajax Engine for UserSpice V0.1 - Ponchale - 06-17-2016

What do you think ?