Is markAllCompleted implemented as a computed for example purposes? It seems like the checkbox checked binding should be a function, and the usage in addItem should be a regular observable.
Just curious really. I’m still fairly noobish when it comes to Breeze and Knockout… In fact, maybe this is a Knockout question more than a Breeze question…
(for reference, I’ve included the relevant code below)
I hate asking questions when I feel like the answer should be obvious, but it’s not. And I couldn’t resist asking.
Thanks!
function addItem() {
var item = dataservice.createTodo();
item.IsDone(vm.markAllCompleted());
item.Description(vm.newTodo());
item.CreatedAt(new Date());
if (item.entityAspect.validateEntity()) {
extendItem(item);
vm.items.push(item);
dataservice.saveChanges();
vm.newTodo("");
} else {
handleItemErrors(item);
}
}
vm.markAllCompleted = ko.computed({
read: function () {
var state = getStateOfItems();
return state.itemsLeftCount === 0 && vm.items().length > 0;
},
write: function (value) {
suspendItemSave = true;
vm.items().forEach(function (item) {
item.IsDone(value);
});
suspendItemSave = false;
dataservice.saveChanges();
}
<input id="markAll" type="checkbox" data-bind="checked: markAllCompleted">
I think the confusion here is caused mostly from an improper naming for the computed value
markAllCompleted. The alternative way you described (having the click bound to a function) is relevant for a button (stateless), not for a checkbox (which has a state of its own, true or false).The (arguably) more proper name for this computed would be something like
allItemsDone. A simpler implementation would not let you write to this value (i.e. it’d be a simple read-only computed, and in the view it’d be a readonly checkbox or equivalent). But since the demo implementation does have this feature (to let the user check/uncheck the master checkbox) then the computed needs to have a write function too, but it can stay a computed, just not a simple read-only one.