Skip to content

Re-enables buttons and links after going back #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,29 @@

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

// This event works the same as the load event, except that it fires every
// time the page is loaded.
//
// See https://github.com/rails/jquery-ujs/issues/357
// See https://developer.mozilla.org/en-US/docs/Using_Firefox_1.5_caching
$(window).on("pageshow.rails", function () {
$($.rails.enableSelector).each(function () {
var element = $(this);

if (element.data("ujs:enable-with")) {
$.rails.enableFormElement(element);
}
});

$($.rails.linkDisableSelector).each(function () {
var element = $(this);

if (element.data("ujs:enable-with")) {
$.rails.enableElement(element);
}
});
});

$document.delegate(rails.linkDisableSelector, 'ajax:complete', function() {
rails.enableElement($(this));
});
Expand Down
30 changes: 30 additions & 0 deletions test/public/test/data-disable-with.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ asyncTest('form input[type=submit][data-disable-with] disables', 6, function(){
}, 30);
});

test('form input[type=submit][data-disable-with] re-enables when `pageshow` event is triggered', function(){
var form = $('form:not([data-remote])'), input = form.find('input[type=submit]');

App.checkEnabledState(input, 'Submit');

// Emulate the disabled state without submitting the form at all, what is the
// state after going back on firefox after submitting a form.
//
// See https://github.com/rails/jquery-ujs/issues/357
$.rails.disableFormElements(form);

App.checkDisabledState(input, 'submitting ...');

$(window).trigger('pageshow');

App.checkEnabledState(input, 'Submit');
});

asyncTest('form[data-remote] input[type=submit][data-disable-with] is replaced in ajax callback', 2, function(){
var form = $('form:not([data-remote])').attr('data-remote', 'true'), origFormContents = form.html();

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

test('a[data-disable-with] re-enables when `pageshow` event is triggered', function() {
var link = $('a[data-disable-with]');

App.checkEnabledState(link, 'Click me');

link.trigger('click');
App.checkDisabledState(link, 'clicking...');

$(window).trigger('pageshow');
App.checkEnabledState(link, 'Click me');
});

asyncTest('a[data-remote][data-disable-with] disables and re-enables', 6, function() {
var link = $('a[data-disable-with]').attr('data-remote', true);

Expand Down