Skip to content
This repository was archived by the owner on Jan 5, 2021. It is now read-only.

Commit 60eb6a8

Browse files
committed
Merge pull request rails#385 from sobrinho/master
Re-enables buttons and links after going back
2 parents 60aadb1 + b2787d4 commit 60eb6a8

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/rails.js

+23
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,29 @@
322322

323323
$.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }});
324324

325+
// This event works the same as the load event, except that it fires every
326+
// time the page is loaded.
327+
//
328+
// See https://github.com/rails/jquery-ujs/issues/357
329+
// See https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching
330+
$(window).on("pageshow.rails", function () {
331+
$($.rails.enableSelector).each(function () {
332+
var element = $(this);
333+
334+
if (element.data("ujs:enable-with")) {
335+
$.rails.enableFormElement(element);
336+
}
337+
});
338+
339+
$($.rails.linkDisableSelector).each(function () {
340+
var element = $(this);
341+
342+
if (element.data("ujs:enable-with")) {
343+
$.rails.enableElement(element);
344+
}
345+
});
346+
});
347+
325348
$document.delegate(rails.linkDisableSelector, 'ajax:complete', function() {
326349
rails.enableElement($(this));
327350
});

test/public/test/data-disable-with.js

+30
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ asyncTest('form input[type=submit][data-disable-with] disables', 6, function(){
9999
}, 30);
100100
});
101101

102+
test('form input[type=submit][data-disable-with] re-enables when `pageshow` event is triggered', function(){
103+
var form = $('form:not([data-remote])'), input = form.find('input[type=submit]');
104+
105+
App.checkEnabledState(input, 'Submit');
106+
107+
// Emulate the disabled state without submitting the form at all, what is the
108+
// state after going back on firefox after submitting a form.
109+
//
110+
// See https://github.com/rails/jquery-ujs/issues/357
111+
$.rails.disableFormElements(form);
112+
113+
App.checkDisabledState(input, 'submitting ...');
114+
115+
$(window).trigger('pageshow');
116+
117+
App.checkEnabledState(input, 'Submit');
118+
});
119+
102120
asyncTest('form[data-remote] input[type=submit][data-disable-with] is replaced in ajax callback', 2, function(){
103121
var form = $('form:not([data-remote])').attr('data-remote', 'true'), origFormContents = form.html();
104122

@@ -173,6 +191,18 @@ asyncTest('a[data-disable-with] disables', 4, function() {
173191
start();
174192
});
175193

194+
test('a[data-disable-with] re-enables when `pageshow` event is triggered', function() {
195+
var link = $('a[data-disable-with]');
196+
197+
App.checkEnabledState(link, 'Click me');
198+
199+
link.trigger('click');
200+
App.checkDisabledState(link, 'clicking...');
201+
202+
$(window).trigger('pageshow');
203+
App.checkEnabledState(link, 'Click me');
204+
});
205+
176206
asyncTest('a[data-remote][data-disable-with] disables and re-enables', 6, function() {
177207
var link = $('a[data-disable-with]').attr('data-remote', true);
178208

0 commit comments

Comments
 (0)