Skip to content

Commit 7b06828

Browse files
committed
Added semaphore disabling of the form button.
1 parent 4c0d070 commit 7b06828

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/form.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,33 @@ AjaxResource.Form = function(form, options) {
1717

1818
this._on_save = options.on_save;
1919

20-
this._semaphore = new AjaxResource.Semaphore();
20+
this._semaphore = new AjaxResource.Semaphore(function() {
21+
var old_submit_value = null;
22+
return {
23+
on_unavailable: function() {
24+
if (old_submit_value === null) {
25+
self.submit_button().attr("disabled", true);
26+
27+
old_submit_value = self.submit_button().val();
28+
self.submit_button().val("Processing...");
29+
} else {
30+
// do nothing since it has been disabled
31+
}
32+
},
33+
on_available: function() {
34+
if (old_submit_value !== null) {
35+
self.submit_button().val(old_submit_value);
36+
self.submit_button().attr("disabled", false);
37+
old_submit_value = null;
38+
} else {
39+
// do nothing since already available
40+
}
41+
}
42+
};
43+
}());
44+
45+
// make sure the submit button is not disabled
46+
this.submit_button().attr("disabled", false);
2147

2248
this.submit_button().bind("click", function(event) {
2349
event.preventDefault();

test/form_test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ jQuery(document).ready(function() {
5858
same(form.parse_fields(), { foo : { bar : "foobar" }, bar : { zeta : "zetar" } }, "Should parse the input fields correct");
5959
});
6060

61+
test("Should correctly initialize semaphore", function() {
62+
var semaphore = form._semaphore;
63+
ok(typeof semaphore !== "undefined", "Should define semaphore");
64+
65+
var submit_value = form.submit_button().val();
66+
equals(form.submit_button().attr("disabled"), false, "The submit button should not be disabled");
67+
68+
semaphore.on_unavailable();
69+
equals(form.submit_button().attr("disabled"), true, "Should disable the submit button");
70+
equals(form.submit_button().val(), "Processing...", "Should change the button inscription");
71+
72+
semaphore.on_available();
73+
equals(form.submit_button().attr("disabled"), false, "Should re-enable the button");
74+
equals(form.submit_button().val(), submit_value, "Should restore original button value");
75+
});
76+
6177
module("#submit", {
6278
setup : function() {
6379
model = {

0 commit comments

Comments
 (0)