, . , :
, , 2 , .
, :
(Fiddle)
function CheckboxedTextbox(checkboxValue, textboxValue) {
this.textboxValue = ko.observable(textboxValue);
this.checkboxValue = ko.computed(function() {
return this.textboxValue();
}, this);
this.isSelected = ko.observable(checkboxValue);
}
function ViewModel() {
this.checkboxes = ko.observableArray([
new CheckboxedTextbox(false),
new CheckboxedTextbox(true, "Default value?"),
new CheckboxedTextbox(false)
]);
this.valid = ko.computed(function() {
return this.checkboxes().filter(function(c) {
return c.isSelected();
}).length <= 2;
}, this);
}
ko.applyBindings(new ViewModel());
, . , , , , . , , , :
(Fiddle)
this.lastChangedBox = ko.observable();
this.forceQuantityChecked = function(newlyChecked) {
setTimeout(function() {
if (!this.valid()) {
this.checkboxes().filter(function(c) {
return c.isSelected() && c !== this.lastChangedBox() && c !== newlyChecked;
}.bind(this)).forEach(function(c) {
c.isSelected(false);
});
}
this.lastChangedBox(newlyChecked);
}.bind(this), 0);
};
setTimeout.
, , - , , observable computed s: " " data: , , .