Accueil > > > PLUGIN JQUERY ARTE: AJOUTER DU TEMPS REEL SUR VOS PAGES WEB
PLUGIN JQUERY ARTE: AJOUTER DU TEMPS REEL SUR VOS PAGES WEB
Information sur la source
Description
Il s'agit d'un plugin JQuery. Celui ci étend les fonctionnalités de l'Ajax en automatisant un certain nombre d'actions. Il gère une boucle en arrière plan, configurable, afin d'exécuter des requêtes Ajax régulièrement. Cela permet essentiellement d'ajouter des fonctionnalités 'temps réel' à votre page web. Ex: mettre à jour une DIV toutes les secondes <div id="elt_html"></div> Et bien, il suffit maintenant d'ajouter deux lignes en javascript: $.arte({'ajax_url':'remote_xml_file_ur l', 'ajax_type':'xml'}); $("elt_html").arte("xml_node" ); La première ligne servant à initialiser le moteur du plugin La deuxième spécifie que l'on souhaite mettre à jour l'élément HTML 'elt_html'. Il dispose de nombreuses autres fonctionnalités plus avancées telles que le parseur automatique de la réponse XML de la requète Ajax, l'arrêt/relance de la boucle à n'importe quel moment (grâce aux routines '.start' et '.stop').
Source
- /*
- * plugin jquery: Ajax Real Time Extension
- *
- * @author arthur.obriot
- *
- * @version 1.5
- * project site: http://plugins.jquery.com/project/Arte
- * developing website: http://code.google.com/p/arte/
- *
- */
-
- (function() {
-
- var _config = null;
- var _is_started = false;
- var _list_xml_actions = new Array();
- var _list_html_node = new Array();
-
- // manage parameters
- // this function test if parameters are not already initialized
- function _manage_parameter(setting) {
- var config = {
- 'time': 1000, // int Timer tick beetwen each round
- 'ajax_mode': 'POST', // GET|POST like the jquery ajax data_mode
- 'ajax_type': 'text', // text|xml like the jquery ajax data_type
- 'ajax_url': '', // url of the ajax request
- 'on_data_set': null, // routine which has to be called before the ajax request (usefull to set ajax parameter). It has to return a string like "arg1=toto&arg2=titi"
- 'on_success': null // routine(text, xml) which will be called when the loop end a round
- };
-
- // is it the first time we have called arte ?
- if (_config == null)
- {
- // if the user has custom options, update ours
- if (setting) $.extend(config, setting);
-
- // set the options of configuration as definitly set
- _config = config;
- }
- }
-
- // start the action loop
- function _start()
- {
- if (_is_started == false)
- {
- _is_started = true;
- _launch_loop();
- }
- return this;
- }
-
- // stop the action loop
- function _stop()
- {
- if (_is_started == true) _is_started = false;
- return this;
- }
-
- // start or stop the action loop, with alternance
- function _toogle()
- {
- if (_is_started == true)
- _is_started = false;
- else
- {
- _is_started = true;
- _launch_loop();
- }
- }
-
- // used to manage parameters after the initialisation of arte
- function _set(key, value)
- {
- if (_config[key])
- _config[key] = value;
- return this;
- }
-
- // used to get parameter values
- function _get(key)
- {
- if (_config[key])
- return _config[key];
- if (key == "nb_node")
- return _list_xml_actions.length;
- if (key == "nb_elt")
- return _list_html_node.length;
- }
-
- // main routine, used to execute one round more
- function _launch_loop()
- {
- // verify we are allowed to launch a new round
- if (_is_started == false) return;
-
- // create the next package to send
- var ajax_data = (_config['on_data_set']) ? _config['on_data_set']() : "";
-
- // set the ajax request
- $.ajax({
- type: _config['ajax_mode'],
- url: _config['ajax_url'],
- data: ajax_data,
- dataType: _config['ajax_type'],
- success: function(data, textStatus){
- // execute the custom list of action with some xml nodes
- if (_list_xml_actions.length > 0)
- _execute_custom_xml_actions(data);
- // auto refresh the list of html element
- if (_list_html_node.length > 0)
- _execute_custom_html_nodes(data);
- // call the final success function
- if (_config['on_success'])
- _config['on_success'](data);
- }
- });
-
- // launch automatically a new cycle
- setTimeout("$.arte().launch()", _config['time']);
- }
-
- // this function, which takes the xml response from the ajax query, parse the response
- // to execute user action
- function _execute_custom_xml_actions(data_xml)
- {
- for (i = 0; i < _list_xml_actions.length; i++)
- $(data_xml).find(_list_xml_actions[i].node_name).each(function (){
- if (_list_xml_actions[i].fct)
- _list_xml_actions[i].fct(this);
- });
- }
-
- // this function, which takes the xml response from the ajax query, parse the response
- // to set automatically some custom html fields
- function _execute_custom_html_nodes(data_xml)
- {
- for (i = 0; i < _list_html_node.length; i++)
- {
- $(data_xml).find(_list_html_node[i].node_name).each(function (){
- $(_list_html_node[i].elt).text($(this).text());
- });
- }
- }
-
- // this function is used to add a custom action to be executed after a success ajax request
- // 'node_name' is name of the xml node we want to get the value
- // 'fct' the function which will be called if we find this node
- function _add_custom_xml_actions(node_name, fct)
- {
- // test if the element doesn't already exist.
- for (i = 0; i < _list_xml_actions.length; i++)
- if (_list_xml_actions[i].node_name == node_name)
- return this;
-
- // add the element in the tab
- _list_xml_actions.push(new _XmlItem(node_name, fct));
- return this;
- }
-
- // same as above but remove the node_name from the action list
- function _del_custom_xml_actions(node_name)
- {
- // rebuild a new clean action list
- var newtab = new Array();
- for (i = 0; i < _list_xml_actions.length; i++)
- if (_list_xml_actions[i].node_name != node_name)
- newtab.push(_list_xml_actions[i]);
- // update the custom list
- _list_xml_actions = newtab;
-
- return this;
- }
-
- $.arte = (function (settings)
- {
- // manage parameters
- _manage_parameter(settings);
-
- // make public links with hidden functions
- return {
- start: _start,
- is_started: _is_started,
- stop: _stop,
- toogle: _toogle,
- set: _set,
- get: _get,
- launch: _launch_loop,
- add_action: _add_custom_xml_actions,
- del_action: _del_custom_xml_actions
- };
- });
-
- // this function is used to make an auto update of a custom html field
- // 'node_name' is name of the xml node we want to use to udpate the html field
- // if no arguement is passed, stop the auto refresh for these nodes
- $.fn.arte = (function (node_name)
- {
- // parse the list of html node
- this.each(function() {
- if (node_name)
- {
- // add the element, and test if the element doesn't already exist
- var is_in = false;
- for (i = 0; i < _list_html_node.length; i++)
- if (_list_html_node[i].elt == this)
- is_in = true;
- if (is_in == false)
- _list_html_node.push(new _HtmlAuto(this, node_name));
- }
- else
- {
- // remove the element
- var newtab = new Array();
- for (i = 0; i < _list_html_node.length; i++)
- if (_list_html_node[i].elt != this)
- newtab.push(_list_html_node[i]);
- // update the custom list
- _list_html_node = newtab;
- }
- });
-
- return this;
- });
-
- // the following class is just a container to join a string with a function
- // it will be used for the xml actions
- function _XmlItem(node_name, fct)
- {
- this.node_name = node_name;
- this.fct = fct;
- }
-
- // the following class is just a container to join an html element with a string
- // it will be used for the auto update html field
- function _HtmlAuto(elt, node_name)
- {
- this.elt = elt;
- this.node_name = node_name;
- }
- })(jQuery);
/*
* plugin jquery: Ajax Real Time Extension
*
* @author arthur.obriot
*
* @version 1.5
* project site: http://plugins.jquery.com/project/Arte
* developing website: http://code.google.com/p/arte/
*
*/
(function() {
var _config = null;
var _is_started = false;
var _list_xml_actions = new Array();
var _list_html_node = new Array();
// manage parameters
// this function test if parameters are not already initialized
function _manage_parameter(setting) {
var config = {
'time': 1000, // int Timer tick beetwen each round
'ajax_mode': 'POST', // GET|POST like the jquery ajax data_mode
'ajax_type': 'text', // text|xml like the jquery ajax data_type
'ajax_url': '', // url of the ajax request
'on_data_set': null, // routine which has to be called before the ajax request (usefull to set ajax parameter). It has to return a string like "arg1=toto&arg2=titi"
'on_success': null // routine(text, xml) which will be called when the loop end a round
};
// is it the first time we have called arte ?
if (_config == null)
{
// if the user has custom options, update ours
if (setting) $.extend(config, setting);
// set the options of configuration as definitly set
_config = config;
}
}
// start the action loop
function _start()
{
if (_is_started == false)
{
_is_started = true;
_launch_loop();
}
return this;
}
// stop the action loop
function _stop()
{
if (_is_started == true) _is_started = false;
return this;
}
// start or stop the action loop, with alternance
function _toogle()
{
if (_is_started == true)
_is_started = false;
else
{
_is_started = true;
_launch_loop();
}
}
// used to manage parameters after the initialisation of arte
function _set(key, value)
{
if (_config[key])
_config[key] = value;
return this;
}
// used to get parameter values
function _get(key)
{
if (_config[key])
return _config[key];
if (key == "nb_node")
return _list_xml_actions.length;
if (key == "nb_elt")
return _list_html_node.length;
}
// main routine, used to execute one round more
function _launch_loop()
{
// verify we are allowed to launch a new round
if (_is_started == false) return;
// create the next package to send
var ajax_data = (_config['on_data_set']) ? _config['on_data_set']() : "";
// set the ajax request
$.ajax({
type: _config['ajax_mode'],
url: _config['ajax_url'],
data: ajax_data,
dataType: _config['ajax_type'],
success: function(data, textStatus){
// execute the custom list of action with some xml nodes
if (_list_xml_actions.length > 0)
_execute_custom_xml_actions(data);
// auto refresh the list of html element
if (_list_html_node.length > 0)
_execute_custom_html_nodes(data);
// call the final success function
if (_config['on_success'])
_config['on_success'](data);
}
});
// launch automatically a new cycle
setTimeout("$.arte().launch()", _config['time']);
}
// this function, which takes the xml response from the ajax query, parse the response
// to execute user action
function _execute_custom_xml_actions(data_xml)
{
for (i = 0; i < _list_xml_actions.length; i++)
$(data_xml).find(_list_xml_actions[i].node_name).each(function (){
if (_list_xml_actions[i].fct)
_list_xml_actions[i].fct(this);
});
}
// this function, which takes the xml response from the ajax query, parse the response
// to set automatically some custom html fields
function _execute_custom_html_nodes(data_xml)
{
for (i = 0; i < _list_html_node.length; i++)
{
$(data_xml).find(_list_html_node[i].node_name).each(function (){
$(_list_html_node[i].elt).text($(this).text());
});
}
}
// this function is used to add a custom action to be executed after a success ajax request
// 'node_name' is name of the xml node we want to get the value
// 'fct' the function which will be called if we find this node
function _add_custom_xml_actions(node_name, fct)
{
// test if the element doesn't already exist.
for (i = 0; i < _list_xml_actions.length; i++)
if (_list_xml_actions[i].node_name == node_name)
return this;
// add the element in the tab
_list_xml_actions.push(new _XmlItem(node_name, fct));
return this;
}
// same as above but remove the node_name from the action list
function _del_custom_xml_actions(node_name)
{
// rebuild a new clean action list
var newtab = new Array();
for (i = 0; i < _list_xml_actions.length; i++)
if (_list_xml_actions[i].node_name != node_name)
newtab.push(_list_xml_actions[i]);
// update the custom list
_list_xml_actions = newtab;
return this;
}
$.arte = (function (settings)
{
// manage parameters
_manage_parameter(settings);
// make public links with hidden functions
return {
start: _start,
is_started: _is_started,
stop: _stop,
toogle: _toogle,
set: _set,
get: _get,
launch: _launch_loop,
add_action: _add_custom_xml_actions,
del_action: _del_custom_xml_actions
};
});
// this function is used to make an auto update of a custom html field
// 'node_name' is name of the xml node we want to use to udpate the html field
// if no arguement is passed, stop the auto refresh for these nodes
$.fn.arte = (function (node_name)
{
// parse the list of html node
this.each(function() {
if (node_name)
{
// add the element, and test if the element doesn't already exist
var is_in = false;
for (i = 0; i < _list_html_node.length; i++)
if (_list_html_node[i].elt == this)
is_in = true;
if (is_in == false)
_list_html_node.push(new _HtmlAuto(this, node_name));
}
else
{
// remove the element
var newtab = new Array();
for (i = 0; i < _list_html_node.length; i++)
if (_list_html_node[i].elt != this)
newtab.push(_list_html_node[i]);
// update the custom list
_list_html_node = newtab;
}
});
return this;
});
// the following class is just a container to join a string with a function
// it will be used for the xml actions
function _XmlItem(node_name, fct)
{
this.node_name = node_name;
this.fct = fct;
}
// the following class is just a container to join an html element with a string
// it will be used for the auto update html field
function _HtmlAuto(elt, node_name)
{
this.elt = elt;
this.node_name = node_name;
}
})(jQuery);
Conclusion
Ajouter du temps réel sur une page web est maintenant à la portée de tous.
Pour plus d'informations et de documentations, vous pouvez visiter le site de développement http://code.google.com/p/arte/ Je suis bien sur à l'écoute de vos remarques et suggestions pour le modifier/corriger.
Historique
- 27 janvier 2010 17:01:31 :
- Correction de quelques fautes d'orthographes
- 29 janvier 2010 03:19:11 :
- edit the title
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
MultiFile.js JQuery plugin... aide à la compréhension de l'utilisation du script [ par polothentik ]
Bonjour,Je suis très intéressé par le script MultiFile.js, plugin de JQuery qui permet l'upload de plusieurs fichiers simultanément. D'après ce que j'
jQuery : Problème de double requete ajax [ par neoseals ]
Bonjour à tous,Voilà je suis sur ce probleme depuis hier après-midi mais impossible de comprendre pourquoi ça ne marche pas ! Contexte:
Zend Jquery Ajax | probleme d'affichege d'element sous IE [ par matdev62 ]
Bonjour, j'ai developpé une application Zend sous wamp. Pour différents formulaires, j'utilise JQuery pour executer des requetes ajax afin d'affiche
JQuery - ordre d'execution des actions ajax [ par matdev62 ]
Bonjour à tous, j'utilise JQuery[ajax] pour gérer l'affichage de tableaux d'informations au parcours des champs d'un formulaire. Au focus, j'affiche
[jQuery] Faire sortir une variable de $.ajax [ par kilhom ]
Bonjour à tous ! Voilà je suis débutant en javascript et j'ai quelque soucis avec une fonction (ajax) de jQuery. J'aimerais pouvoir faire sortir la va
JQuery / JQuery lightBox Plugin : icône de chargement toujours affichée ??? [ par ABDev ]
Bonjour, Pour le développement de mon prochain site, j'ai décidé d'utiliser, pour l'affichage des photographies des utilisateurs, [url=http://leandrov
Jquery - LightBox : pb avec l'overlay [ par Masterfoums ]
Bonjour, Après avoir longuement chercher sur le net une réponse à mon soucis je n'ai pas trouvé de solution. Je suis en train de créer un petit site
S'initier à JQuery/Ajax/ASP [ par sylvain64520 ]
Salut à tous, Je commence dans JQuery donc naturellement j'ai des questions. En ce qu'il concerne les BDD. Je vois partout des connexions aux BDD à
plugin wslide jquery [ par am1ra2 ]
bonjour, j'ai utilisé le pluguin wslide de jquery pour le défilement des pages html,tout va bien,ce que je veux est que le défilement soit juste au pa
|
Derniers Blogs
CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT)CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT) par FREMYCOMPANY
Bonjour à tous, Je viens de publier une proposition comprenant 5 pseudo-classes pour le CSS Working Group ayant trait à l'état de chargement d'un élément (ex: IMG,VIDEO,AUDIO,OBJECT pour l'HTML.). Si le c½ur vous en dit, vous pouvez retrouver cette p...
Cliquez pour lire la suite de l'article par FREMYCOMPANY MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks [HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL[HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL par Pierrick CATRO-BROUILLET
Avec la sortie prochaine de la Beta Consumer Preview de Windows 8, j'avais envie de revenir sur une des fonctionnalités que j'attends le plus et que, en bon geek que je suis, j'utilise déjà : Hyper-V 3 ainsi son module PowerShell.
Il y a déjà pléthor...
Cliquez pour lire la suite de l'article par Pierrick CATRO-BROUILLET IIS7 - COMPRESSION GZIPIIS7 - COMPRESSION GZIP par cyril
La compression GZIP permet d'améliorer les performances de navigation en compressant ce qu'envoie le serveur à un client. Pour comprendre comment cela fonctionne, regardons ce qu'il se passe au niveau HTTP lorsqu'un client tente d'accéder à une ress...
Cliquez pour lire la suite de l'article par cyril
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|