0

I am trying to use knockbackjs (knockoutjs + backbonejs) where the model is a 2d array )ie table, and the viewModel is binded using foreach to a table html tag, so I will get an observable table, each click on a cell will send the entire 2d array to the api.

I've been looking at this and that examples with a question I've asked and got to something like this:

    //model
    var GameModel = Backbone.Model.extend({ urlRoot: '/game' });

    //viewmodel
    var GameViewModel = function (game) {

        this.board = kb.observable(game, [
                         [ko.observable(0), ko.observable(0), ko.observable(0)],
                         [ko.observable(0), ko.observable(0), ko.observable(0)],
                         [ko.observable(0), ko.observable(0), ko.observable(0)]
                     ]);
    };

    var model = new GameModel({ id: 1 });
    var gameViewModel = new GameViewModel(model);
    ko.applyBindings(gameViewModel);

but I get ' Uncaught Observable: key is missing ' on the line starting with

this.board = kb.observable(game, [

How can I fix this? Is there a better way of achieving this?

Community
  • 1
  • 1
Mithir
  • 2,355
  • 2
  • 25
  • 37

1 Answers1

0

You need to send an object to the observable, I'd have thought, whereas you are just sending through a list of parameters.

You either need to just send the array in, or else make it an object, e.g.:

this.board = kb.observable(
{
    'game': game,
    'board': [
        [ko.observable(0), ko.observable(0), ko.observable(0)],
        [ko.observable(0), ko.observable(0), ko.observable(0)],
        [ko.observable(0), ko.observable(0), ko.observable(0)]
    ]
});
Paul Manzotti
  • 5,107
  • 21
  • 27
  • it gives me this error- options is missing. when I've put some options I get the error key is missing. which key am I missing? – Mithir Apr 12 '13 at 20:12
  • What is options referring to? Is that something you are binding to in the html? – Paul Manzotti Apr 12 '13 at 22:16
  • options that are in the kb.observable function parameters. http://kmalakoff.github.io/knockback/tutorial_kb_observable.html , it fails before the binding part. – Mithir Apr 13 '13 at 04:22