好吧,你可以像这样在原型上创建一个minLength
函数:observableArray
ko.observableArray.fn.stringMinLength = function (minLength) {
var self = this;// observableArray
// Loop through array and apply logic to each element
// Add subscriptions to the observableArray to determine what's been added (don't care about what's been removed)
// In the subscriptions, deal with how to apply the minLength validation
// -can't use '.extend({ minLength: minLength })' because these are plain strings, not observables
return self;
}
现在你可以这样做了:
var oa = ko.observableArray(["foo", "bar", "bo"]).stringMinLength(3);
oa
您的问题的症结在于,当' 的值发生突变时,您希望如何对所有字符串元素应用验证。