Last active
March 28, 2018 11:54
-
-
Save hawkidoki/b0642e20f8e5c4c6b45242cfd04cfb2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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