我正在寻找一个淘汰赛验证插件,我偶然发现了knockout.validation,它看起来很有希望,但是,它有一个致命的缺陷..
一旦你创建了一个 ko.validatedObservable({ name: foo }) 你就不能给这个 observable 分配一个新对象。
例如:在我的视图模型中,我实例化了一个经过验证的 observable。
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 现在包含来自服务器的值,但是,当我运行时
item().isValid(); //False is returned
这在 GitHub 项目https://github.com/ericmbarnard/Knockout-Validation/issues?state=open上列为错误 #209 。
有谁知道优雅的工作?或者另一个可以实现这个目标的插件?