app/assets/javascripts/trestle/components/_form.js in trestle-0.8.9 vs app/assets/javascripts/trestle/components/_form.js in trestle-0.8.10
- old
+ new
@@ -7,17 +7,28 @@
Trestle.init(function(e, root) {
var form = $(root).find('form[data-behavior="trestle-form"]');
form
+ .on('ajax:send', function(e, xhr) {
+ // Disable submit buttons
+ $(this).find(':submit').prop('disabled', true);
+
+ // Set loading status on button that triggered submission
+ var button = $(this).data('trestle:submitButton');
+ if (button) { $(button).addClass('loading'); }
+ })
.on('ajax:complete', function(e, xhr, status) {
- var contentType = xhr.getResponseHeader("Content-Type").split(";")[0];
+ // Reset submit buttons
+ $(this).find(':submit').prop('disabled', false).removeClass('loading');
+ $(this).removeData('trestle:submitButton');
- if (contentType == "text/html") {
+ var contentType = xhr.getResponseHeader("Content-Type");
+
+ if (contentType && contentType.split(";")[0] == "text/html") {
if (/<html/i.test(xhr.responseText)) {
// Response is a full HTML page, likely an error page. Render within an iframe.
-
var context = $(this).closest('[data-context]');
var iframe = $("<iframe>").addClass('error-iframe').get(0);
context.html(iframe);
iframe.contentWindow.document.documentElement.innerHTML = xhr.responseText;
@@ -34,20 +45,20 @@
}
} else {
// Assume an error response
var title = xhr.status + " (" + xhr.statusText + ")";
Trestle.Dialog.showError(title, xhr.responseText);
-
- // Reset submit button
- form.find(':submit').prop('disabled', false).removeClass('loading');
}
})
.on('ajax:success', function(e, data, status, xhr) {
var context = $(this).closest('[data-context]');
var location = xhr.getResponseHeader("X-Trestle-Location");
if (location) {
+ // Retain current active tab
+ location = location + document.location.hash;
+
// Update the URL in the browser and context
history.replaceState({}, "", location);
context.data('context', location);
}
@@ -55,17 +66,10 @@
if (!context.hasClass('app-main')) {
Trestle.refreshMainContext();
}
});
- // Loading indicator
form.find(':submit').click(function() {
- var button = $(this);
-
- // Delay to ensure form is still submitted
- setTimeout(function() {
- if (form[0].checkValidity()) {
- button.prop('disabled', true).addClass('loading');
- }
- }, 1);
+ // Save this as the button that triggered the form
+ $(this).closest('form').data('trestle:submitButton', this);
});
});