KnockoutJS - 使用键/值对的映射

时间:2014-08-20 09:58:22

标签: javascript jquery knockout.js

我需要在Knockout JS中使用java.util.Map。 Map条目的key始终是表单输入的路径/名称; value是错误消息,应显示在表单输入旁边:

例如:MyObject.getErrors().push("myObjects[0].myField1"), "This field is mandatory!");

如何在Knockout JS中检查Map键是否等于输入名称,如果是,则在表单输入中附加“error <span>”?

甚至有可能吗?

JavaObject:

public class MyObject {
    public String myField1;
    public String myField2;
    private Map<String, String> errors = new HashMap<String, String>();
}

淘汰赛对象:

function MyObject(data) {
    this.field1 = ko.observable(data.field1);
    this.field1 = ko.observable(data.field1);
    this.errors = [{
        ?
    }]
}

JSON响应:

{
    "class": "MyObjectHelper",
    "myObjects": [
        {
            "class": "MyObject",
            "errors": {

            },
            "field1": "ABC123",
            "field2": "01.01"
        },
        {
            "class": "com.itensis.leina16.application.data.domain.Configuration",
            "errors": {
                "myObjects[1].field2": "Is required"
            },
            "field1": "ASDF123",
            "field2": ""
        },
        {
            "class": "com.itensis.leina16.application.data.domain.Configuration",
            "errors": {
                "configurations[2].field1": "Is required",
                "configurations[2].field2": "Is required"
            },
            "field1": "",
            "field2": ""
        }
    ],
    "hasErrors": true
}

0 个答案:

没有答案