2013-03-18 102 views
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場均計算的字段從身份代碼中獲得。我只想知道如何在完成之前獲得身份代碼的驗證結果。謝謝!

回答

0

的驗證可觀察被擴展與計算isValid。因此,您可以檢查與結果:

self.gender = ko.computed(function() { 
     // get gender information from the identiy code here 
     if(self.identityCode.isValid()) { 
      // do something with the code 
     } 
    });