【问题标题】:Mapping an object that contains an observablearray that contains an observablearray映射包含 observablearray 的对象,该对象包含 observablearray
【发布时间】:2013-11-24 22:35:39
【问题描述】:

谁能指出为什么下面的映射不能正常工作?该表以正确的行数正确呈现。 <td data-bind="text: Rank"></td> 也按预期解析,但没有其他数据绑定成功。但是,调试器没有返回任何错误。

Json

[{"Rank":1,"IntegerList":{"IntegerListID":14,"Direction":"Ascending","Performance":451,"Integers":[{"IntegerValue":0},{ "IntegerValue":0}]}},{"Rank":2,"IntegerList":{"IntegerListID":13,"Direction":"Ascending","Performance":504,"Integers":[{"IntegerValue ":0},{"IntegerValue":0}]}},{"Rank":3,"IntegerList":{"IntegerListID":18,"Direction":"Ascending","Performance":514,"Integers ":[{"IntegerValue":1},{"IntegerValue":2}]}},{"Rank":4,"IntegerList":{"IntegerListID":19,"Direction":"Ascending","Performance ":515,"整数":[{"IntegerValue":2},{"IntegerValue":3},{"IntegerValue":4}]}}]

淘汰赛

function rankedIntegerLists(data) {
    this.Rank = data.Rank;
    this.IntegerList = ko.observableArray([this.IntegerListID = data.IntegerListID,
                                           this.Performance = data.Performance,
                                           this.Direction = data.Direction,
                                           this.Integers = ko.observableArray(data.Integers)]);
    }

$.getJSON("/Home/GetIntegerLists", function (allData) {
    var mappedIntegers = $.map(allData, function (item) { return new rankedIntegerLists(item) });
    viewModel.integerlists(mappedIntegers);
});

viewModel.integerlists = ko.observableArray([]);

HTML

<tbody data-bind="foreach: integerlists">
    <tr>
        <td data-bind="text: Rank"></td>
        <td>
            <ul class="integers" data-bind="foreach: IntegerList.Integers">
                <li data-bind="text: IntegerValue"></li>
            </ul>
        </td>
        <td data-bind="text: IntegerList.Direction"></td>
        <td data-bind="text: IntegerList.Performance"></td>
    </tr>
</tbody>

编辑

ko.toJSON($data, null, 2) 返回以下内容。显然是 IntegerList observablearray 没有被映射。

"integerlists": [
{
  "Rank": 1,
  "IntegerList": [
    null,
    null,
    null,
    null
  ]
},
{
  "Rank": 2,
  "IntegerList": [
    null,
    null,
    null,
    null
  ]
},
{
  "Rank": 3,
  "IntegerList": [
    null,
    null,
    null,
    null
  ]
},
{
  "Rank": 4,
  "IntegerList": [
    null,
    null,
    null,
    null
  ]
},
{
  "Rank": 5,
  "IntegerList": [
    null,
    null,
    null,
    null
  ]
}]

【问题讨论】:

  • 将此添加到您的视图中,它可能有助于调试已映射的值 -
     在你的 foreach 里面。
  • 啊哈——这很有用。几乎证实了我的想法(见编辑过的问题)。我不确定如何解决这个问题。
  • 更好的是,为 Knockout 安装这个 Chrome 扩展 - chrome.google.com/webstore/detail/knockoutjs-context-debugg/… 它可以让您检查页面上任何元素的上下文。对我的 Knockout 开发非常宝贵。
  • 在这种情况下并不是更好 - 如果映射扩展没有映射属性,那么上下文将永远不会分配跟踪器并观察值......但它是一个有用的工具。

标签: json knockout.js knockout-mapping-plugin


【解决方案1】:

我并不是说这是您唯一的问题或要专门解决您的问题,但我认为在这种情况下使用模型进行调试和代码清晰是更好的做法。此外,在您的代码中使用 var self = this 以确保您仍然处于正确的范围/上下文中。

function integerListModel(data) {
   var self = this;
   self.IntegerListID = ko.observable(data.IntegerListID);
   self.Performance = ko.observable(data.Performance);
   self.Direction = ko.observable(data.Direction);
   self.Integers = ko.observableArray(data.Integers);
}

function rankedIntegerLists(data) {
    var self = this;
    self.Rank = data.Rank;
    self.IntegerList = new integerListModel(data);
}

如果您仍然没有获得数据,请尝试更新您的代码并在其中添加一些 console.log,这可能会帮助您找出原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 2018-08-02
    • 2021-08-16
    • 2021-11-22
    相关资源
    最近更新 更多