我有对项目进行验证的 ko.observableArrays。用户可以将项目标记为已删除。当它被标记为已删除时,我需要禁用对该项目的验证。
如何动态禁用验证?
示例:http: //jsfiddle.net/3RZjT/2/
<div data-bind="foreach: names">
<input data-bind="value: name, valueUpdate: 'afterkeydown'" /> <a data-bind="click: deleteMe, text:deleted()?'undelete':'delete'" href="#">delete</a><br/>
</div>
function Person(name){
var self = this;
self.name = ko.observable(name).extend({ required: true});
self.deleted = ko.observable(false);
self.deleteMe = function(){
self.deleted(!self.deleted());
self.deleted.extend({ validatable: !self.deleted()});
};
}
var viewModel = {
names: ko.observableArray([new Person("Ken"), new Person("")])
};
ko.applyBindings(viewModel);