More resistant javascript code

This commit is contained in:
Raphael Michel
2016-11-08 20:28:16 +01:00
parent b30a3b5bee
commit ddf769e55d
5 changed files with 106 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
/*global $, waitingDialog, failsafe_gettext */
/*global $, waitingDialog, gettext */
var async_task_id = null;
var async_task_timeout = null;
var async_task_check_url = null;
@@ -10,7 +10,7 @@ function async_task_check() {
'type': 'GET',
'url': async_task_check_url,
'success': async_task_check_callback,
'error': async_task_error,
'error': async_task_check_error,
'context': this,
'dataType': 'json'
}
@@ -24,11 +24,32 @@ function async_task_check_callback(data, jqXHR, status) {
return;
}
async_task_timeout = window.setTimeout(async_task_check, 250);
$("#loadingmodal p").text(gettext('Your request has been queued on the server and will now be ' +
'processed.'));
}
function async_task_check_error(jqXHR, textStatus, errorThrown) {
"use strict";
var c = $(jqXHR.responseText).filter('.container');
if (c.length > 0) {
waitingDialog.hide();
ajaxErrDialog.show(c.first().html());
} else {
if (jqXHR.status >= 400 && jqXHR.status < 500) {
waitingDialog.hide();
alert(gettext('An error of type {code} occured.').replace(/\{code\}/, jqXHR.status));
} else {
// 500 can be an application error or overload in some cases :(
$("#loadingmodal p").text(gettext('We currenctly cannot reach the server, but we keep trying.' +
' Last error code: {code}').replace(/\{code\}/, jqXHR.status));
async_task_timeout = window.setTimeout(async_task_check, 5000);
}
}
}
function async_task_callback(data, jqXHR, status) {
"use strict";
$(this).data('ajaxing', false);
$("body").data('ajaxing', false);
if (data.redirect) {
location.href = data.redirect;
return;
@@ -36,17 +57,26 @@ function async_task_callback(data, jqXHR, status) {
async_task_id = data.async_id;
async_task_check_url = data.check_url;
async_task_timeout = window.setTimeout(async_task_check, 100);
history.pushState({}, "Waiting", async_task_check_url.replace(/ajax=1/, ''));
}
function async_task_error(jqXHR, textStatus, errorThrown) {
"use strict";
waitingDialog.hide();
// TODO: Handle status codes != 200
$("body").data('ajaxing', false);
var c = $(jqXHR.responseText).filter('.container');
if (c.length > 0) {
waitingDialog.hide();
ajaxErrDialog.show(c.first().html());
} else {
alert(failsafe_gettext('Unknown error.'));
if (jqXHR.status >= 400 && jqXHR.status < 500) {
waitingDialog.hide();
alert(gettext('An error of type {code} occured.').replace(/\{code\}/, jqXHR.status));
} else {
waitingDialog.hide();
alert(gettext('We currenctly cannot reach the server. Please try again. ' +
'Error code: {code}').replace(/\{code\}/, jqXHR.status));
}
}
}
@@ -54,11 +84,12 @@ $(function () {
"use strict";
$("body").on('submit', 'form[data-asynctask]', function (e) {
e.preventDefault();
if ($(this).data('ajaxing')) {
if ($("body").data('ajaxing')) {
return;
}
$(this).data('ajaxing', true);
waitingDialog.show(failsafe_gettext('We are processing your request …'));
async_task_id = null;
$("body").data('ajaxing', true);
waitingDialog.show(gettext('We are processing your request …'));
$.ajax(
{

View File

@@ -1,4 +1,4 @@
/*global $,failsafe_gettext,failsafe_ngettext */
/*global $,gettext,ngettext */
var cart = {
_deadline: null,
@@ -7,10 +7,10 @@ var cart = {
draw_deadline: function () {
var diff = Math.floor(cart._deadline.diff(moment()) / 1000 / 60);
if (diff < 0) {
$("#cart-deadline").text(failsafe_gettext("The items in your cart are no longer reserved for you."));
$("#cart-deadline").text(gettext("The items in your cart are no longer reserved for you."));
window.clearInterval(cart._deadline_interval);
} else {
$("#cart-deadline").text(failsafe_ngettext(
$("#cart-deadline").text(ngettext(
"The items in your cart are reserved for you for one minute.",
"The items in your cart are reserved for you for {num} minutes.",
diff

View File

@@ -1,12 +1,12 @@
/*global $ */
function failsafe_gettext(msgid) {
function gettext(msgid) {
if (typeof django !== 'undefined' && typeof django.gettext !== 'undefined') {
return django.gettext(msgid);
}
return msgid;
}
function failsafe_ngettext(singular, plural, count) {
function ngettext(singular, plural, count) {
if (typeof django !== 'undefined' && typeof django.ngettext !== 'undefined') {
return django.ngettext(singular, plural, count);
}
@@ -54,7 +54,7 @@ var ajaxErrDialog = {
"use strict";
$("#ajaxerr").html(c);
$("#ajaxerr .links").html("<a class='btn btn-default ajaxerr-close'>"
+ failsafe_gettext("Close message") + "</a>");
+ gettext("Close message") + "</a>");
$("body").addClass("ajaxerr");
},
hide: function () {