mardi 30 juin 2015

knockout - computed observable with parameter working only on alternate clicks

I have a main checkbox, clicking on which all the checkboxes are checked. For the child checkboxes, I have some different logic involving their computation from 2 different observables besides the mainCheckBox and when child checkboxes are checked, the value needs to be written to different observables.

I have created a computed observable with parameter as below:

var vm = ko.mapping.fromJS(data);
var viewModel = 
function () 
{
    this.self = this;
    this.ExportSchemas = vm;
    this.SelectAll = ko.observable(true);
    this.IsChecked = function (checkboxVM) {
        return ko.computed({
            read: function () {
                return this.SelectAll(); //problemLine
            },
            write: function (value) {
                checkboxVM.Selected(value);
                alert(checkboxVM.Selected());
            },
            owner: this.self
        });
    };
};

<input type="checkbox" data-bind="checked: SelectAll" value="None" id="mainCheckBox" name="check" />

<div data-bind="foreach: ExportSchemas">
    <div data-bind="foreach: Clauses">
        <input type="checkbox" value="None" data-bind="checked: $root.IsChecked($data), attr: { id: 'clause' + Id(), value: Id() }" name="check" />
    </div>
</div>

The write behavior of computed observable is weird, it works on alternate clicks when the problemLine in above code is as written above. And when I use the following instead for problemLine:

return this.SelectAll;

Then the write of computed observable works properly, but the the read stops working and checking/unchecking mainCheckBox has no effect on child check boxes.

Any suggestions?




Aucun commentaire:

Enregistrer un commentaire