Okay, unless someone has a better "KnockoutJS" way of doing it to
teach a newbie like me, I have a cleaner solution that works, though I
STILL have to incorporate view logic into the model script to disable
the radios when the checkbox is checked for some reason. I'd love to
know a solution that doesn't require this. But I now use a regular
checkbox with the jqmChecked binding handler as discussed earlier,
then use the subscribe to handle the inter-field relationships which
was also discussed in another thread. The difference now is I convert
the data upon loading and before saving so the model always uses
booleans for the checkboxes but still handles and supplies 0 or 1
strings for communicating with the existing back-end.
var viewModel = {
parent: ko.observable({
child: ko.observable({
bigCheck:ko.observable(false),
fieldOne:ko.observable(""),
fieldTwo:ko.observable(""),
fieldThree:ko.observable("")
})
})
};
// checkbox & radio buttons handler
ko.bindingHandlers.jqmChecked = {
init: ko.bindingHandlers.checked.init,
update: function(element, valueAccessor) {
ko.bindingHandlers.checked.update(element, valueAccessor);
$(element).checkboxradio("refresh");
}
};
// relationships
viewModel.parent().child().bigCheck.subscribe(function(newValue) {
if(newValue) {
viewModel.parent().child().fieldOne(1);
viewModel.parent().child().fieldTwo(0);
viewModel.parent().child().fieldThree(0);
}
// need to update radios for some reason
$('input:radio[name*=field]').attr('disabled',
newValue).checkboxradio("refresh");
});
// then where data is loaded via Ajax
viewModel.parent().child().bigCheck(data.subdata.bigCheck=='1'?
true:false);
// and where it is prepped to save back to the server
var tmp = ko.toJSON(viewModel.parent().child);
tmp.bigCheck = bigCheck?'1':'0';
============
<input type="checkbox" name="bigCheck" id="bc" data-bind="jqmChecked:
parent().child().bigCheck" />
<label for="bc">Big Checkbox</label>
<fieldset data-role="controlgroup">
<legend><b>Field 1:</b></legend>
<!-- the disabled binding does not work here even when applied to
all -->
<input type="radio" name="fieldOne" id="carry-radio-choice-6.5"
value="0" data-bind="jqmChecked: parent().child().fieldOne, disabled:
parent().child().bigCheck" />