0

I'm really new to Knockout and have a question: I try this example from the officail site.

So my html file is:

<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>

and my js file is:

/// <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));

What am I missing? I debug the code I saw this error "UncauchType error: cannot read property 'viewModel' of undefined" tnx

zvi
  • 3,677
  • 2
  • 30
  • 48
  • 4
    possible duplicate of [Does the simpleGrid require additional downloads?](http://stackoverflow.com/questions/18507885/does-the-simplegrid-require-additional-downloads) – Nathan Sep 23 '14 at 13:15

1 Answers1

0

You are not using ko.applyBindings() anywhere.

ideally you need to do something like this.

ko.applyBindings(new PagedGridModel(initialData));
QBM5
  • 2,778
  • 2
  • 17
  • 24
  • 1
    I copied this `ko.applyBindings(new PagedGridModel(initialData));` from the last line of his question. – edhedges Sep 23 '14 at 13:55