Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

knockout.js - Knockout validation hello world not running on jsfiddle

I'm trying to ask a question about async knockout.js validation running on page load, and I'm attempting to reproduce the issue on jsfiddle.

Thing is, I can't get the most basic example to work, despite having a more complex scenario running on my box. What's wrong with this?

http://jsfiddle.net/C5rSm/4/

I have to post code:

<div id="vm">
    <input type="text" data-bind="value: validatableField" />
    <p data-bind="validationMessage: validatableField"></p>
    <button data-bind="click: alertValue">value is alerted ok, but doesn't validate</button>
</div>

var Vm = function(){
    var self = this;
    self.validatableField = ko.observable().extend({ equal: 2 });
    self.alertValue = function(){
        alert(self.validatableField());
    };
};

ko.applyBindings(new Vm(), document.getElementById('vm'));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is nothing wrong with your code.

However the current version of the validation plugin on (cdnjs 1.0.2) is quite old and it has a bug which prevents the ko.validation.registerExtenders working correctly. This bug has been fixed since then.

As a workaround you need to call ko.validation.registerExtenders() at the start of your fiddle:

ko.validation.registerExtenders();
var Vm = function(){
    var self = this;
    self.validatableField = ko.observable().extend({ equal: "2" });
    self.alertValue = function(){
        alert(self.validatableField());
    };
};

ko.applyBindings(new Vm(), document.getElementById('vm'));

Demo JSFiddle.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...