gpt4 book ai didi

javascript - knockout 验证的可观察数据绑定(bind)未按预期工作

转载 作者:行者123 更新时间:2023-11-28 01:32:12 25 4
gpt4 key购买 nike

我正在尝试将数据绑定(bind)到 validObservable,但该控件似乎不像其他 knockout 可观察对象那样可更新。

在这里摆弄,http://jsfiddle.net/EricHerlitz/x7UUg/

简短说明

// variable with the data bound observable
that.validationErrors = ko.validatedObservable();

// Elements to validate
var validationGroup = {
email1: that.email1,
firstName: that.firstName
};

// Trying to use the validatedObservable in a normal knockout way doesn't work
that.validationErrors(validationGroup);

// This will fill the variable with the observable result but as usual when performing this pattern the viewmodel must be rebound.
that.validationErrors = ko.validatedObservable(validationGroup);

来源

<h3>Registrering</h3>
<div class="form-group">
<label for="Email1">E-postadress</label>
<input data-bind="value: email1" id="Email1" class="form-control" placeholder="E-postadress (du kommer få lösenords-länk skickad hit)" />
</div>
<div class="form-group">
<label for="Firstname">Förnamn</label>
<input data-bind="value: firstName" id="Firstname" class="form-control" placeholder="Förnamn" />
</div>

<button type="button" class="btn btn-primary" data-bind="click: registerClick">Registrera</button>

<div class="alert alert-danger" data-bind="visible: !validationErrors.isValid()">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4>There be dragons!</h4>
<div data-bind="foreach: validationErrors.errors">
<div data-bind="text: $data"></div>
</div>
</div>

JS

var GK = GK || {};
GK.VM = GK.VM || {};

GK.VM.Member = function (template) {
/*** Private variables ***/
var that = this;

/*** Public variables ***/
that.validationErrors = ko.validatedObservable();

// variables with validation, https://github.com/Knockout-Contrib/Knockout-Validation
that.email1 = ko.observable().extend({
required: { params: true, message: "missingEmail" },
pattern: { params: /^([\d\w-\.]+@([\d\w-]+\.)+[\w]{2,4})?$/, message: "wrongEmailFormat" }
});
that.firstName = ko.observable().extend({
required: { params: true, message: "missingFirstName" }
});

that.registerClick = function () {

// Elements to validate
var validationGroup = {
email1: that.email1,
firstName: that.firstName
};

// This would be ideal but doesn't work
//that.validationErrors(validationGroup);
//console.log(that.validationErrors.errors()); // does not contain any erros

// This will destroy the data-bind but register the validatedObservable
// Requires rebinding of the control :(
that.validationErrors = ko.validatedObservable(validationGroup);
console.log(that.validationErrors.errors()); // contains errors
};
};

var viewModel = new GK.VM.Member();
ko.applyBindings(viewModel);

有什么建议可以处理这个问题吗?谢谢。

最佳答案

目前,您无法在创建 validatedObservable 后更新它,除非完全重新创建它。

但是,如果您不需要向其添加删除属性的动态方面,而只需要根据条件显示收集的验证消息(当调用 registerClick 时),那么还有其他选择解决方案。

一种解决方案是正常创建validatedObservable:

that.validationErrors = ko.validatedObservable({
email1: that.email1,
firstName: that.firstName
});

然后添加一个新的可观察值:

that.showErrorMessages = ko.observable();

并在用户界面中使用它

<div class="alert alert-danger" 
data-bind="visible: showErrorMessages() && !validationErrors.isValid()">

并在 registerClick 中切换何时显示验证消息:

that.registerClick = function () {
that.showErrorMessages(true);
};

演示 JSFiddle .

关于javascript - knockout 验证的可观察数据绑定(bind)未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21985435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com