0

observableArray私のモデルは非常に単純 (文字列のリスト) であり、型指定されたオブジェクトを入れてから、 deep validation を使用して検証したくないとしますko.validation.init( { grouping: { deep: true } } )

たとえば、すべての配列要素 (プレーンな文字列) に準拠する必要がありminLength:3ます。

ko.observable().extend({minLength:3})大丈夫なのにko.observableArray().extend({minLength:3})動かないのはなぜ?

(プレーン文字列の代わりに) オブジェクトを配列に入れる以外の回避策はありますか?

から始めるフィドル。

4

1 に答える 1

1

さて、次のようにプロトタイプに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問題の核心は、の値が変化したときにすべての文字列要素に検証を適用する方法です。

于 2013-09-10T19:30:14.770 に答える