Permalink
Browse files

Added semaphore disabling of the form button.

  • Loading branch information...
1 parent 4c0d070 commit 7b06828a8ea8ecf979aa3bfbad68c178eaee0129 @ratnikov committed Mar 4, 2009
Showing with 43 additions and 1 deletion.
  1. +27 −1 src/form.js
  2. +16 −0 test/form_test.js
View
@@ -17,7 +17,33 @@ AjaxResource.Form = function(form, options) {
this._on_save = options.on_save;
- this._semaphore = new AjaxResource.Semaphore();
+ this._semaphore = new AjaxResource.Semaphore(function() {
+ var old_submit_value = null;
+ return {
+ on_unavailable: function() {
+ if (old_submit_value === null) {
+ self.submit_button().attr("disabled", true);
+
+ old_submit_value = self.submit_button().val();
+ self.submit_button().val("Processing...");
+ } else {
+ // do nothing since it has been disabled
+ }
+ },
+ on_available: function() {
+ if (old_submit_value !== null) {
+ self.submit_button().val(old_submit_value);
+ self.submit_button().attr("disabled", false);
+ old_submit_value = null;
+ } else {
+ // do nothing since already available
+ }
+ }
+ };
+ }());
+
+ // make sure the submit button is not disabled
+ this.submit_button().attr("disabled", false);
this.submit_button().bind("click", function(event) {
event.preventDefault();
View
@@ -58,6 +58,22 @@ jQuery(document).ready(function() {
same(form.parse_fields(), { foo : { bar : "foobar" }, bar : { zeta : "zetar" } }, "Should parse the input fields correct");
});
+ test("Should correctly initialize semaphore", function() {
+ var semaphore = form._semaphore;
+ ok(typeof semaphore !== "undefined", "Should define semaphore");
+
+ var submit_value = form.submit_button().val();
+ equals(form.submit_button().attr("disabled"), false, "The submit button should not be disabled");
+
+ semaphore.on_unavailable();
+ equals(form.submit_button().attr("disabled"), true, "Should disable the submit button");
+ equals(form.submit_button().val(), "Processing...", "Should change the button inscription");
+
+ semaphore.on_available();
+ equals(form.submit_button().attr("disabled"), false, "Should re-enable the button");
+ equals(form.submit_button().val(), submit_value, "Should restore original button value");
+ });
+
module("#submit", {
setup : function() {
model = {

0 comments on commit 7b06828

Please sign in to comment.