Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hawkidoki/b0642e20f8e5c4c6b45242cfd04cfb2d to your computer and use it in GitHub Desktop.
Save hawkidoki/b0642e20f8e5c4c6b45242cfd04cfb2d to your computer and use it in GitHub Desktop.
jQuery(document).ready(function($){
// Vérification si un container de module existe
if($('[data-hwk-ajax-action-id]').length){
// Traiter chaque module indépendamment
$('[data-hwk-ajax-action-id]').each(function(){
// Variables générales
var $this = $(this),
$id = $this.data('hwk-ajax-action-id'),
$ts_cache = $this.data('hwk-ajax-ts'),
$param = $this.data('hwk-ajax-param'),
$ts_current = Math.floor(Date.now() / 1000),
// Données envoyées en Ajax
$data = {
'action': 'hwk_ajax_template',
'id': $id
};
// Si un param est défini, le faire remonter dans la requête Ajax
if($param != '')
$data.param = $param;
// Ne pas lancer de requête Ajax si le cache n'est pas expiré
if($ts_cache > $ts_current)
return;
// Requête Ajax
$.ajax({
url: ajaxurl,
type: 'post',
data: $data,
success: function(ajax){
// Error
if(!ajax.success || !ajax.data)
return;
// Remplacer le contenu du module par la version à jour
$this.replaceWith(ajax.data);
return;
}
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment