0

我有一个定义如下的视图模型:

var ViewModel = function() {
    var self = this;
    self.name = ko.observable().extend({ required: true });
    self.identityCode = ko.observable().extend({ required: true, maxLength: 18, minLength: 15 });
    self.gender = ko.computed(function() {
        // get gender information from the identiy code here
    });
    self.birthdate = ko.computed(function() {
       // get birthdate information from the identity code here
    });
    self.form_onsubmit = function (form) {
        if (!self.isValid()) {
            self.errors.showAllMessages();
            return false;
        } else {
            return true;
        }
    };
};

正如您在上面的代码中看到的,性别字段和 brithdate 字段是从身份代码中获取的计算字段。我只是想知道如何在做之前获得身份代码的验证结果。谢谢!

4

1 回答 1

0

已验证的 observables 使用计算扩展isValid。因此,您可以通过以下方式检查结果:

    self.gender = ko.computed(function() {
        // get gender information from the identiy code here
        if(self.identityCode.isValid()) {
          // do something with the code
        }
    });
于 2013-03-20T18:58:12.023 回答