komapping.toJS不映射嵌套的observable数组

时间:2017-02-03 10:45:20

标签: knockout.js knockout-mapping-plugin

我有JSON作为嵌套数组,如下所示

return Arrays.stream(dimensions)
             .collect(() -> new int[4], (array, dimension) -> {
        array[0] = Math.max(array[0], dimension.startX);
        array[1] = Math.min(array[1], dimension.endX);
        array[2] = Math.max(array[2], dimension.startY);
        array[3] = Math.min(array[3], dimension.endY);
}, (a, b) -> {
        a[0] = Math.max(a[0], b[0]);
        a[1] = Math.min(a[1], b[1]);
        a[2] = Math.max(a[2], b[2]);
        a[3] = Math.min(a[3], b[3]);
});
  • 然后我按客户编号和entityEntityName进行分组并使用哪个填充模型可观察数组让我跟随JSON

     [
      {
        "Count": 2,
        "CustomerAccountNumber": 1,
        "Entities": [
          {
            "Count": 1,
            "Entity": [
              [object Object]
            ],
            "EntityName": "a",
            "Sum": 3250
          },
          {
            "Count": 1,
            "Entity": [
              [object Object]
            ],
            "EntityName": "b",
            "Sum": 450
          }
        ],
        "Sum": 3700
      },
      {
        "Count": 1,
        "CustomerAccountNumber": 2,
        "Entities": [
          {
            "Count": 1,
            "Entity": [
              [object Object]
            ],
            "EntityName": "c",
            "Sum": 5600
          }
        ],
        "Sum": 5600
      }
    ] 

    然后当我需要返回JS时,TOJs不会映射嵌套数组,但是html中的模型绑定与knockout正确绑定数据。

komapping.toJS(viewModel.result())

给我以下结果 - 它没有映射嵌套数组,有什么办法可以解决这个问题吗?

更新

   [
                  {
                    "Amount": 3250,
                    "CustomerAccountNumber": 1,
                    "EntityName": "a",
                    "Id": 1,
                    "IsValidationSuccess": false,
                    "IsWorkInProgress": true,
                    "ValidationErrors": [
                      {
                        "CreationDate": "2017-01-23T00:00:00",
                        "Reason": "Error1"
                      },
                      {
                        "CreationDate": "2017-01-23T00:00:00",
                        "Reason": "error2"
                      }
                    ]
                  },
                  {
                    "Amount": 450,
                    "CustomerAccountNumber": 1,
                    "EntityName": "s",
                    "Id": 4,
                    "IsValidationSuccess": false,
                    "IsWorkInProgress": true,
                    "ValidationErrors": [
                      {
                        "CreationDate": "2017-01-23T00:00:00",
                        "Reason": "error"
                      }
                    ]
                  },
                  {
                    "Amount": 5600,
                    "CustomerAccountNumber": 2,
                    "EntityName": "f",
                    "Id": 2,
                    "IsValidationSuccess": false,
                    "IsWorkInProgress": true,
                    "ValidationErrors": [
                      {
                        "CreationDate": "2017-01-23T00:00:00",
                        "Reason": "error"
                      }
                    ]
                  }
                ]

在AJAX加载之后我在分组后将对象推送到observableArray

viewModel = function () {
    var self = this;
    self.transactionLines = ko.observableArray();
    self.result = ko.observableArray();

}

1 个答案:

答案 0 :(得分:0)

查看ko.mapping.toJS() documentation,这并不令人惊讶:

  

这将创建一个仅包含属性的未映射对象   作为原始JS对象一部分的映射对象。所以   换句话说,您手动添加到的任何属性或功能   您的视图模型被忽略

如果您正在查看导出对象,我认为您可能需要尝试ko.toJS()ko.toJSON()而不是ko.mapping.toJS()

请参阅documentation for ko.toJS()