Knockout asp.net - 无法读取undefined的属性

时间:2014-09-23 13:12:11

标签: asp.net knockout.js

我是Knockout的新手并且有一个问题: 我在官方网站上尝试this示例。

所以我的html文件是:

<head>
    <script src="Scripts/jquery-2.1.1.min.js"></script>
    <script src="Scripts/knockout-3.2.0.js"></script>
</head>
<body>
        <div data-bind='simpleGrid: gridViewModel'> </div>

        <button data-bind='click: addItem'>
            Add item
        </button>

        <button data-bind='click: sortByName'>
            Sort by name
        </button>

        <button data-bind='click: jumpToFirstPage, enable: gridViewModel.currentPageIndex'>
            Jump to first page
        </button>
    <script src="FuseJS.js"></script>
</body>
</html>

我的js文件是:

/// <reference path="Scripts/jquery-2.1.1.min.js" />
/// <reference path="Scripts/knockout-3.2.0.js" />
var initialData = [
    { name: "Well-Travelled Kitten", sales: 352, price: 75.95 },
    { name: "Speedy Coyote", sales: 89, price: 190.00 },
];

var PagedGridModel = function (items) {
    this.items = ko.observableArray(items);

    this.addItem = function () {
        this.items.push({ name: "New item", sales: 0, price: 100 });
    };

    this.sortByName = function () {
        this.items.sort(function (a, b) {
            return a.name < b.name ? -1 : 1;
        });
    };

    this.jumpToFirstPage = function () {
        this.gridViewModel.currentPageIndex(0);
    };

    this.gridViewModel = new ko.simpleGrid.viewModel({
        data: this.items,
        columns: [
            { headerText: "Item Name", rowText: "name" },
            { headerText: "Sales Count", rowText: "sales" },
            { headerText: "Price", rowText: function (item) { return "$" + item.price.toFixed(2) } }
        ],
        pageSize: 4
    });
};

ko.applyBindings(new PagedGridModel(initialData));

我错过了什么?我调试代码我看到了这个错误 “UncauchType错误:无法读取未定义的属性'viewModel'” TNX

1 个答案:

答案 0 :(得分:0)

您没有在任何地方使用ko.applyBindings()。

理想情况下,你需要做这样的事情。

ko.applyBindings(new PagedGridModel(initialData));