Me gustaría saber si hay un mejor enfoque para crear objetos ajax reutilizables para jquery.Patrón JS ajax reutilizable (jquery)
Este es mi código no probado.
var sender = {
function ajax(url, type, dataType, callback) {
$.ajax({
url: url,
type: type,
dataType: dataType,
beforeSend: function() {
onStartAjax();
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
callback.failure(XMLHttpRequest, textStatus, errorThrown);
},
success: function(data, textStatus) {
callback.success(data, textStatus);
},
complete: function (XMLHttpRequest, textStatus) {
onEndAjax();
}
});
},
function onStartAjax() {
// show loader
},
function onEndAjax() {
// hide loader
}
};
<script type="text/javascript">
var callback = {
success: function(data, textStatus) {
$('#content').html(data);
},
failure: function(XMLHttpRequest, textStatus, errorThrown) {
alert('Error making AJAX call: ' + XMLHttpRequest.statusText + ' (' + XMLHttpRequest.status + ')');
}
}
sender.ajax(url, type, dataType, callback);
</script>
es posible pensar en volver en sí la promesa en su sender.ajax ('volver $ .ajax ({...}) ') para que pueda adjuntar otras devoluciones de llamada del cliente. Por supuesto, si prefieres ocultar por completo el ajax de jquery del otro código, no deberías hacer eso. – Stefan