Skip to content

Commit

Permalink
handle ajax errors (closes foodcoops#300)
Browse files Browse the repository at this point in the history
Conflicts:
	app/assets/javascripts/application.js
  • Loading branch information
wvengen committed Jul 2, 2014
1 parent 37a0105 commit 05f84f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
29 changes: 9 additions & 20 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,15 @@ $(function() {
return false;
});

// Show and hide loader on ajax callbacks
// and run newElementsReady() afterwards for new dom elements
$(document).on('ajax:beforeSend', '[data-remote]', function(evt, xhr, settings) {
$('#loader').show();
// One idea was $(document).on('ajax:complete', '[data-remote'], ...)
// but that doesn't work for a modal dialog that replaces itself.
// https://github.com/rails/jquery-ujs/issues/223
if (!settings.complete)
settings.complete = [];
else if (!$.isArray(settings.complete))
settings.complete = [settings.complete];
settings.complete.push(function() {
newElementsReady();
$('#loader').hide();
});
});

// Disable submit button on ajax forms
$(document).on('ajax:beforeSend', 'form[data-remote]', 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
Expand Down
1 change: 1 addition & 0 deletions config/i18n-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 05f84f0

Please sign in to comment.