Knockout - 绑定单击嵌套数组子元素

时间:2012-11-13 16:30:28

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

好的,我在使用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中的click事件。当我加载我的页面时,我收到错误“无法解析绑定。未定义updateStatus”。

我假设我的问题出现在我的项目映射中。如果我删除了单击处理程序,则所有内容都会绑定并正确显示。

如果有人可以帮助我,也许可以指出我所缺少的东西,这将是非常有帮助的。我对Knockout的东西有点新鲜,所以很可能我错过了一些明显/简单的东西。

(不,我的小提琴中的数据都不是敏感的或真实的,只是虚拟数据。:))

1 个答案:

答案 0 :(得分:3)

我确实在the KnockoutJs JabbR chat room中回答了你的问题。

你的假设是正确的。问题似乎主要是你的映射。你甚至都不会调用PaycheckBudget类的构造函数。例如,您的BudgetListViewModel构造函数如下所示:

var BudgetListViewModel = function(data) {
    var self = this;
    self.budgets = ko.observableArray(data);
};

但应该是这样的:

var BudgetListViewModel = function(data) {
    var self = this;
    self.budgets = ko.observableArray(ko.utils.arrayMap(data, function(item){
        return new PaycheckBudget(item);
    }));
};

我已经编辑了你的jsfiddle,并在http://jsfiddle.net/KBpET/处有一份工作样本。我首先编辑了另一个映射,所以我从未检查项目映射是否正确,但我确信你将能够解决该步骤(项目的代码已在小提琴链接中更改)。< / p>

希望它有所帮助!

/罗伯特