简体   繁体   English

在剔除JS中绑定复杂模型

[英]Bind complex model in knockout JS

I have the following JSON complex object. 我有以下JSON复杂对象。

{"User":
    {
    "$id":"2",
    "Id":0,
    "FirstName":null,
    "LastName":null,
    "Email":null,
    "EmailConfirmed":false,
    "PasswordHash":null,
    }
}

How to bind this object in knockout js. 如何在淘汰赛js中绑定此对象。 So far I have used somethind like this to bind property with input field. 到目前为止,我已经使用诸如此类的东西将属性与输入字段绑定。

<input required class="form-control" data-bind="value: User.FirstName" type="text" />

Functions bo build model in knockout. 函数bo在剔除中建立模型。

function userModel() {
        var self = this;
        self.User = ko.observable();
    }

    function bindData(data) {
        userInfo.User(data["User"]);
    }

When I call submiting via JS. 当我通过JS调用提交时。

var jsonData = ko.toJSON(userInfo);

Object jsonData show that property like FirstName is null, however in formular value has been written. 对象jsonData表明,诸如FirstName之类的属性为null,但是已在公式值中编写了该属性。 Object userInfo stores written values in formular, I have checked it. 对象userInfo将写入的值存储在公式器中,我已经检查了它。

Should it look like this? 应该是这样吗?

function userModel() {
        var self = this;
        self.Password = ko.observable();
        self.User = ko.observable();
    }

    function UserViewModel(user) {
        this.FirstName = ko.observable(user.FirstName);
        this.LastName = ko.observable(user.LastName);
        // other properties
    }

    function bindData(data) {
        userInfo.Password(data["Password"]);
        userInfo.User(new UserViewModel(data["User"]));
    }

$(document).ready(function () {
        userInfo = new userModel();
        createUser();
        ko.applyBindings(userInfo);
    });

For two way binding to work, you need to build the same hierarchy of observable values on the view model. 为了使双向绑定起作用,您需要在视图模型上构建可观察值的相同层次结构。

Alternatively, you could use the mapping plugin : 或者,您可以使用映射插件

Since User is also a observable, you have to update your binding like so: 由于用户也是可观察的,因此您必须像这样更新绑定:

<input required class="form-control" data-bind="value: User().FirstName" type="text" />

Since User has a lot of properties, you could benefit from the with binding : 由于User具有许多属性,因此您可以从with绑定中受益:

Here's a fiddle with both examples (with and without the parent binding) 这是两个示例的小提琴(带或不带父绑定)

 var data = { "User": { "$id": "2", "Id": 0, "FirstName": "Joseph", "LastName": "Campbell", "Email": null, "EmailConfirmed": false, "PasswordHash": null, } } function UserViewModel(user) { this.FirstName = ko.observable(user.FirstName); this.LastName = ko.observable(user.LastName); // other properties } function bindData(data) { userInfo.User(new UserViewModel(data["User"])); } function userModel() { var self = this; self.User = ko.observable(); } var userInfo = new userModel(); bindData(data); ko.applyBindings(userInfo); 
 input { display: block; margin: 5px 0; } input[readonly] { border: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <form data-bind="with: User"> <input type="text" data-bind="value: FirstName" /> <input type="text" data-bind="value: LastName" /> </form> Current values: <input type="text" readonly data-bind="value: User().FirstName" /> <input type="text" readonly data-bind="value: User().LastName" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM