简体   繁体   English

扩展基类属性

[英]Extending base-class properties

How do I extend properties from a base 'class' in knockout.js? 如何在敲除js的基础“类”中扩展属性?

I'm trying to setup knockout-validation in my view models but it doesn't work unless my 'sub-class' has an observable that is also extended . 我试图在我的视图模型中设置敲除验证,但是除非我的“子类”具有可extended的Observable,否则它将无法正常工作。 See the comments in the code snippet below: 请参见下面的代码片段中的注释:

 // setup knockout validation ko.validation.rules.pattern.message = 'Invalid.'; ko.validation.configure({ registerExtenders: true, messagesOnModified: true, insertMessages: true }); // base 'class' function userAddressModelBase(data) { var self = this; self.firstName = ko.observable(data.firstName); self.lastName = ko.observable(data.lastName); } userAddressModel.prototype = new userAddressModelBase({}); userAddressModel.constructor = userAddressModel; // 'sub-class' function userAddressModel(data) { var self = this; userAddressModelBase.call(self, data); // extend 'base-class' properties; this doesn't work unless the current 'sub-class' also has an observable who is also extended... self.firstName.extend({ required: true }); self.lastName.extend({ required: true }); // the above statement only starts working if I extend an observable from this `sub-class` self.city = ko.observable();//.extend({ required: true }); self.errors = ko.validation.group(self); self.validate = function() { console.log(self.errors().length); if (self.errors().length > 0) { self.errors.showAllMessages(); return; } }; } var model = new userAddressModel({ firstName: "Ted" }); ko.applyBindings(model); 
 .validationMessage{ color: #ea033d; font-weight: 700; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <div> <label>First Name: <input type="text" data-bind="value: firstName" /></label><br> <label>Last Name: <input type="text" data-bind="value: lastName" /></label><br> <label>City: <input type="text" data-bind="value: city" /></label><br> <button data-bind="click: validate">Validate</button> </div> 

The recommended way to perform inheritance is to use ko.utils.extend(self, new userAddressModelBase(data)); 推荐的执行继承的方法是使用ko.utils.extend(self, new userAddressModelBase(data));

function userAddressModelBase(data) {
    var self = this;

    self.firstName = ko.observable(data.firstName);
    self.lastName = ko.observable(data.lastName);
}

Below I am using extend to add required validation to firstName and lastName. 下面,我使用扩展将所需的验证添加到firstName和lastName。

function userAddressModel(data) {
    var self = this;

    ko.utils.extend(self, new userAddressModelBase(data))
    self.firstName.extend({ required: true });
    self.lastName.extend({ required: true });
};

Now in the example if you remove the firstName and then exit the text box you can see that the required message appears. 现在,在示例中,如果删除了firstName,然后退出了文本框,则可以看到出现了所需的消息。

http://jsfiddle.net/d4vLqkx2/1/ http://jsfiddle.net/d4vLqkx2/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM