【发布时间】: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