ノックアウト検証プラグインを探していて、非常に有望に見えるノックアウト.検証に出くわしましたが、致命的な欠陥があります..
ko.validatedObservable({ name: foo }) を作成すると、そのオブザーバブルに新しいオブジェクトを割り当てることはできません。
例: 私のビュー モデルでは、検証済みのオブザーバブルをインスタンス化します。
var item = new ko.validatedObservable(new Tag({}));
その後、次のように呼び出すことができます。
item().isValid(); //Returns false in this case because Tag is empty
タグはこんな感じ
Model.Tag = function (data) {
var
Id = ko.observable(data.Id),
Name = ko.observable(data.Name).extend({ required: true, maxLength: 64 }),
Description = ko.observable(data.Description).extend({ required: true, maxLength: 512 });
return {
Id: Id,
Name: Name,
Description: Description
};
};
問題は、サーバーから新しいタグを取得して、そのタグを変更したい場合です..
$.ajax({
url: API + "/" + id,
type: 'GET',
dataType: 'json',
timeout: Timeout,
statusCode: {
200: function(response) { item(response); }, //Here is where the bug is!
404: ItemNotFound
},
error: function () {
Item(new Type({}));
}
});
アイテムにはサーバーからの値が含まれるようになりましたが、実行すると
item().isValid(); //False is returned
これは、GitHub プロジェクトhttps://github.com/ericmbarnard/Knockout-Validation/issues?state=openのバグ #209 としてリストされています。
エレガントな回避策を知っている人はいますか? または、この目標を達成する別のプラグインですか?