Last active
August 29, 2015 14:01
-
-
Save n0nick/ceeb51937bd9a292f97f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit d1f7317d48aa8e19ca8130448f694794b14d9107 | |
Author: Sagie Maoz <[email protected]> | |
Date: Mon May 19 14:50:19 2014 +0300 | |
FormModelAdapter: Fix updating checkbox in groups | |
Appearantly, jQuery's $.each breaks the loop if its callback function | |
returns `false`. Meanwhile, coffeescript implicitly returns the value of | |
the last line in a function. Thus, if the last assignment happens to be | |
to the value of `false` (for example, for an unchecked checkbox), the loop | |
quits and none of the other attributes are saved. | |
I have to add this explicit `true` return value in order to avoid this. | |
diff --git a/app/assets/javascripts/backbone/form_model_adapter.js.coffee b/app/assets/javascripts/backbone/form_model_adapter.js.coffee | |
index 2146d38..8c7d847 100644 | |
--- a/app/assets/javascripts/backbone/form_model_adapter.js.coffee | |
+++ b/app/assets/javascripts/backbone/form_model_adapter.js.coffee | |
@@ -61,6 +61,7 @@ class W.FormModelAdapter | |
field.each (i, el) => | |
attribute = @_getSubAttributeName($(el)) | |
val[attribute] = @_getFieldValue($(el)) | |
+ true # avoid breaking the loop | |
val | |
# single field | |
diff --git a/spec/javascripts/backbone/form_model_adapter_spec.js.coffee b/spec/javascripts/backbone/form_model_adapter_spec.js.coffee | |
index 1ec1268..42c73cc 100644 | |
--- a/spec/javascripts/backbone/form_model_adapter_spec.js.coffee | |
+++ b/spec/javascripts/backbone/form_model_adapter_spec.js.coffee | |
@@ -100,6 +100,14 @@ describe "W.FormModelAdapter", -> | |
@adapter.updateModel() | |
expect(@model.get('field1')).toEqual { sub1: 'val1', sub2: 'val2' } | |
+ it "updates boolean (checkbox) group fields", -> | |
+ @form.append( | |
+ $('<input name="mymodel[field1_attributes][sub1]" type="checkbox" value="1">') | |
+ $('<input name="mymodel[field1_attributes][sub2]" type="checkbox" value="1" checked="checked">') | |
+ ) | |
+ @adapter.updateModel() | |
+ expect(@model.get('field1')).toEqual { sub1: false, sub2: true } | |
+ | |
describe "when field isn't defined as attribute", -> | |
it "is skipped", -> | |
@form.append( |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment