本文介绍了是否将KnockoutJS验证绑定值传递给自定义绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用KnockoutJS-Validation,无论字段是否通过验证,我都需要传递给自定义绑定.猜测我需要使用allBindingsAccessor参数以某种方式挂接到在字段级别可观察到的KnockoutJS-Validation,但不确定如何.

Using KnockoutJS-Validation, I need to pass to a custom binding whether the field passed validation or not. Guessing I need to somehow hook into a KnockoutJS-Validation observable at field level using the allBindingsAccessor parameter but unsure how.

ko.bindingHandlers.mycustombinding = {
    update: function (element, valueAccessor, allBindingsAccessor) {
        allBindings = allBindingsAccessor(),
        validationObservable = allBindings.validationObservable;    
        if (!validationObservable) {
            //do cool jQuery stuff to the element if it doesn't validate
        }
    }
};

http://jsfiddle.net/hotdiggity/mtwLA/6/

推荐答案

该库在扩展的可观察对象中添加了一个obserabler isValid

The library adds a obserabler isValid to the observable thats extended

http://jsfiddle.net/MCNU8/

var observable = ko.observable("f").extend({ number: true });
console.log(observable.isValid());

这篇关于是否将KnockoutJS验证绑定值传递给自定义绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 17:18