如何从现有的可观察物中移除增量剂? [英] How to remove extender from an existing observable?

查看:58
本文介绍了如何从现有的可观察物中移除增量剂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Knockout Validation插件,并使用扩展程序根据需要设置可观察对象:

I am using the Knockout Validation plugin and setting an observable as required using the extender:

myObservable.extend({required:true});

添加扩展剂后,我是否可以将其移除?

Is it possible for me to remove the extender after adding it?

推荐答案

您可以从ko验证通过调用以下方法删除所有可验证的相关属性:

You can remove all the validation relates properties form an observable which were added by the ko validation with calling:

myObservable.extend({validatable: false});

或者,如果您只想删除所需的验证,则可以将其从rules集合中删除:

Or if you want to only remove the required validation you can remove it from the rules collection:

myObservable.rules.remove(function (item) {
        return item.rule == "required";
    });
}

演示 JSFiddle .

但是ko验证支持条件验证,因此您可以在验证有效时指定一些条件,也许这就是您所需要的:

But the ko validation has support for conditional validation, so you can specify some condition when the validation should work so maybe this is what you need:

myObservable.extend({
    required: {
        message: "Some message",
        onlyIf: function () { return //some condition; }
    }
});

这篇关于如何从现有的可观察物中移除增量剂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆