Skip to content
Open
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: 16 additions & 7 deletions backbone.modelbinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var modelbinding = (function(Backbone, _, $) {

this.registerDataBinding = function(model, eventName, callback){
// bind the model changes to the elements

model.bind(eventName, callback);
this.modelBindings.push({model: model, eventName: eventName, callback: callback});
};
Expand All @@ -83,7 +83,7 @@ var modelbinding = (function(Backbone, _, $) {
modelBinding.Configuration = function(options){
this.bindingAttrConfig = {};

_.extend(this.bindingAttrConfig,
_.extend(this.bindingAttrConfig,
modelBinding.Configuration.bindindAttrConfig,
options
);
Expand All @@ -98,8 +98,8 @@ var modelbinding = (function(Backbone, _, $) {
}
}

this.getBindingAttr = function(type){
return this.bindingAttrConfig[type];
this.getBindingAttr = function(type){
return this.bindingAttrConfig[type];
};

this.getBindingValue = function(element, type){
Expand Down Expand Up @@ -248,7 +248,7 @@ var modelbinding = (function(Backbone, _, $) {
var attr_value = model.get(attribute_name);
if (typeof attr_value !== "undefined" && attr_value !== null) {
element.val(attr_value);
}
}

// set the model to the form's value if there is no model value
if (element.val() != attr_value) {
Expand Down Expand Up @@ -294,9 +294,18 @@ var modelbinding = (function(Backbone, _, $) {

// bind the form changes to the model
var elementChange = function(ev){
var element = view.$(ev.currentTarget);
var element = view.$(ev.currentTarget),
val = element.val(),
modelVal;
if (element.is(":checked")){
setModelValue(group_name, element.val());
if(val === 'true'){
modelVal = true;
} else if(val === 'false') {
modelVal = false;
} else {
modelVal = val;
}
setModelValue(group_name, modelVal);
}
};

Expand Down
14 changes: 14 additions & 0 deletions spec/javascripts/radioButtonConventionBinding.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ describe("radio button convention binding", function(){
expect(this.model.get('graduated')).toEqual("no");
});

it("bind view changes to the model's field, by convention of id (boolean true)", function(){
var el = this.view.$("#us_citizen_true");
el.attr("checked", "checked");
el.trigger('change');
expect(this.model.get('us_citizen')).toBe(true);
});

it("bind view changes to the model's field, by convention of id (boolean false)", function(){
var el = this.view.$("#us_citizen_false");
el.attr("checked", "checked");
el.trigger('change');
expect(this.model.get('us_citizen')).toBe(false);
});

it("bind model field changes to the form input, by convention of id", function(){
this.model.set({graduated: "yes"});
var el = this.view.$("#graduated_yes");
Expand Down