无法在ko.validatedObservable中包装viewmodel

时间:2016-12-02 14:38:06

标签: javascript knockout.js knockout-validation

我想从服务器返回的Json创建包含在ko.validatedObservable中的viewmodel。我已阅读Knockout-validation文档。而不是在那里记录,我想使用映射包装viewmodel,以便isValid,并且错误是viewmodel的一部分,而不是作为额外属性的一部分。

我正在使用以下javascript文件(patientviemodel.js) -

xhr.send

我发现如果我改变了

,就会识别出映射
var patinetViewModelMapping = {
    create: function (options) {
        console.log("mapping");
        var patientVm = ko.mapping.fromJS(options.data, {}, self);
        return ko.validatedObservable(patientVm);
    }
}
PatientViewModel = function (data) {
    var self = this;

    console.log("creating");
    ko.mapping.fromJS(data, patinetViewModelMapping, self);

    self.Name = ko.observable().extend({ required: { params: true, message: 'This field is required.' } });
    //self = ko.validatedObservable(self);
    self.save = function (action) {
        if (!self.isValid()) {
            self.errors.showAllMessages();
            return;
        }
        var ajax = {
            url: "/patientregistration/" + action + "/",
            type: "POST",
            data: ko.toJSON(self),
            contentType: 'application/json',
            success: function (data) {
                if (data.patientViewModel != null) {
                    console.log(data.patientViewModel.Id);
                    ko.mapping.fromJS(data.patientViewModel, {}, self);
                }
                if (data.newLocation != null) {
                    console.log(data.newLocation);
                    window.location = data.newLocation;
                }
            }
        };
        $.ajax(ajax);
    }
}

ko.mapping.fromJS(data, patinetViewModelMapping, self);

但是,尽管viewmodel是按照映射创建的,但是执行在save函数中失败,错误“isValid is not defined。”

调用applybinding发生在视图(.cshtml)中,如下所示 -

ko.mapping.fromJS(data, patinetViewModelMapping);

bower.json包含(添加以供参考版本) -

@model MSPro.Apps.ClinicManager.Web.UI.ViewModel.PatientViewModel
@using System.Web.Script.Serialization
@using Newtonsoft.Json;

@{
    ViewBag.Title = "Register New Patient";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{
    string data = JsonConvert.SerializeObject(Model, new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.IsoDateFormat });//new JavaScriptSerializer().Serialize(Model);
}
Value of json : @data

@section scripts
{
    <script src="~/lib/knockoutjs/dist/knockout.js"></script>
    <script src="~/lib/knockout.mapping/build/output/knockout.mapping-latest.js"></script>
    <script src="~/lib/knockout-validation/dist/knockout.validation.js"></script>

    <script src="~/Scripts/setforgerytoken.js"></script>
    <script src="~/Scripts/patientviemodel.js"></script>
    <script type="text/javascript">
        var patientViewModel = new PatientViewModel(@Html.Raw(data));
        ko.applyBindings(patientViewModel);
    </script>
}
@Html.Partial("_EditPatientRegistration")

<p><button data-bind="click:function(){save('Create')}" class="btn btn-primary">Save</button></p>

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

非常新的Javascript。非常感谢您的帮助。 提前谢谢。

0 个答案:

没有答案