Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...