From d8f4ac47dd2834a8f330648d491cd97f475cc493 Mon Sep 17 00:00:00 2001 From: wvengen Date: Wed, 2 Jul 2014 13:55:05 +0200 Subject: [PATCH] handle ajax errors (closes foodcoops#300) --- app/assets/javascripts/application.js | 21 +++++++++------------ config/i18n-js.yml | 1 + 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index e639b29c..d43b1cb2 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -116,18 +116,15 @@ $(function() { return false; }); - // Show and hide loader on ajax callbacks - $('*[data-remote]').bind('ajax:beforeSend', function() { - $('#loader').show(); - }); - - $('*[data-remote]').bind('ajax:complete', function() { - $('#loader').hide(); - }); - - // Disable submit button on ajax forms - $('form[data-remote]').bind('ajax:beforeSend', function() { - $(this).children('input[type="submit"]').attr('disabled', 'disabled'); + // Handle ajax errors + // render json: {error: "can't except this!"}, status: :unprocessable_entity + $(document).ajaxError(function(ev, xhr, settings, exception) { + try { + msg = xhr.responseJSON.error; + } catch(err) { + msg = I18n.t('errors.general'); + } + alert(msg); }); // The autocomplete attribute is used for both autocompletion and storing diff --git a/config/i18n-js.yml b/config/i18n-js.yml index c1d71d19..1631dc1b 100644 --- a/config/i18n-js.yml +++ b/config/i18n-js.yml @@ -3,6 +3,7 @@ translations: - file: 'app/assets/javascripts/i18n/translations.js' only: [ '*.js.*', + '*.errors.*', '*.number.*', '*.date.formats.*', # foodsoft-specific texts to keep js with normal translations