好的,我在使用 Knockout 时遇到问题,因为它无法解析可观察数组的子元素上的点击绑定。我怀疑我的映射有问题,但我无法弄清楚。我有一个我创建的 jsFiddle:
http://jsfiddle.net/internetH3ro/ShjeE/1/
这是我的 JS:
var PaycheckBudgetItem = function(data) {
var self = this;
self.PaycheckBudgetItemId = ko.observable(data.PaycheckBudgetItemId);
self.PaycheckBudgetId = ko.observable(data.PaycheckBudgetId);
self.Description = ko.observable(data.Description);
self.Amount = ko.observable(data.Amount);
self.DueDate = ko.observable(data.DueDate);
self.IsPaid = ko.observable(data.IsPaid);
self.PaidStatus = ko.observable(data.PaidStatus);
self.updateStatus = function() {
alert('test');
};
};
var mapping = {
'Items': {
create: function(options) {
return new PaycheckBudgetItem(options.data);
}
}
};
var PaycheckBudget = function(data) {
var self = this;
self.PaycheckBudgetId = ko.observable(data.PaycheckBudgetId);
self.PaycheckBudgetDate = ko.observable(data.PaycheckBudgetDate);
self.PaycheckBudgetAmount = ko.observable(data.PaycheckBudgetAmount);
self.Remaining = ko.observable(data.Remaining);
self.Items = ko.observableArray(data.Items);
ko.mapping.fromJSON(data.Items, mapping, self.Items);
};
var BudgetListViewModel = function(data) {
var self = this;
self.budgets = ko.observableArray(data);
};
var data = [
{
"PaycheckBudgetId": 1,
"PaycheckBudgetDate": "11/9/2012",
"PaycheckBudgetAmount": 2315,
"Items": [
{
"PaycheckBudgetItemId": 11,
"PaycheckBudgetId": 1,
"Description": "Rent",
"Amount": 400,
"DueDate": "11/9/2012",
"IsPaid": "Unpaid",
"PaidStatus": false},
{
"PaycheckBudgetItemId": 12,
"PaycheckBudgetId": 1,
"Description": "Cell Phone",
"Amount": 166,
"DueDate": "11/9/2012",
"IsPaid": "Unpaid",
"PaidStatus": false}
],
"Remaining": 1749},
{
"PaycheckBudgetId": 2,
"PaycheckBudgetDate": "11/23/2012",
"PaycheckBudgetAmount": 2315,
"Items": [
{
"PaycheckBudgetItemId": 3,
"PaycheckBudgetId": 2,
"Description": "Rent",
"Amount": 400,
"DueDate": "11/23/2012",
"IsPaid": "Unpaid",
"PaidStatus": false},
{
"PaycheckBudgetItemId": 4,
"PaycheckBudgetId": 2,
"Description": "Tuition",
"Amount": 250,
"DueDate": "11/23/2012",
"IsPaid": "Unpaid",
"PaidStatus": false}
],
"Remaining": 1665}
];
ko.applyBindings(new BudgetListViewModel(data), $('#budget-list-container')[0]);
这是我的 HTML:
<div id="budget-list-container" class="well sidebar-nav">
<ul class="nav nav-list" data-bind="template: { name: 'budgetTemplate', foreach: budgets, as: 'budget' }">
<script type="text/html" id="budgetTemplate">
<li class="nav-header">
Paycheck for <span data-bind="text: PaycheckBudgetDate"></span> - <span data-bind="text: formatCurrency(PaycheckBudgetAmount)"></span>
</li>
<li>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Description</th>
<th>Amount</th>
<th>Due Date</th>
<th>Paid/Unpaid</th>
</tr>
</thead>
<tbody data-bind="template: { name: 'budgetItemTemplate', foreach: Items, as: 'budgetItem' }"></tbody>
</table>
Remaining: <span data-bind="text: formatCurrency(Remaining)"></span>
<hr />
</li>
</script>
<script type="text/html" id="budgetItemTemplate">
<tr>
<td><span data-bind="text: Description"></span></td>
<td><span data-bind="text: formatCurrency(Amount)"></span></td>
<td><span data-bind="text: DueDate"></span></td>
<td>
<button class="btn-link pull-right update-status" data-bind="text: IsPaid, click: updateStatus" />
</td>
</tr>
</script>
</ul>
</div>
它的肉是这个。我有 2 个 KO 模型,PaycheckBudget 和 PaycheckBudgetItem。PaycheckBudget 包含 PaycheckBudgetItem 模型的可观察数组。PaycheckBudgetItem 模型上有一个名为“updateStatus”的函数,它绑定到我的 HTML 中的点击事件。当我加载我的页面时,我收到错误“无法解析绑定。未定义更新状态”。
我假设我的问题出在我的项目映射中。如果我删除点击处理程序,一切都会绑定并正确显示。
如果有人可以帮助我,也许指出我所缺少的,那将非常有帮助。我对淘汰赛的东西有点陌生,所以我很可能错过了一些明显/简单的东西。
(不,我的小提琴中没有任何数据是敏感或真实世界的,只是虚拟数据。:))