9

I want to create an observableArray. The code below prints "6 : 0 " and "3 : 0", i.e. the observableArrays are empty.

I've tested this in Firefox 5 and Chrome 11.0.696.68.

What am I doing wrong?

var myStringArray = new Array( "Bungle", "Bear", "George", "Hippo", "Zippy", "Unknown" );

var myObjectArray = new Array(
  { name: "Bungle", type: "Bear" },
  { name: "George", type: "Hippo" },
  { name: "Zippy", type: "Unknown" }
);

var viewStringModel = ko.observableArray( myStringArray );
var viewObjectModel = ko.observableArray( myObjectArray );

console.log(myStringArray.length + " : " + viewStringModel.length);
console.log(myObjectArray.length + " : " + viewObjectModel.length);

1 Answer 1

7

change:

 console.log(myStringArray.length + " : " + viewStringModel.length);
 console.log(myObjectArray.length + " : " + viewObjectModel.length);

to

 console.log(myStringArray.length + " : " + viewStringModel().length);
 console.log(myObjectArray.length + " : " + viewObjectModel().length);
2
  • also viewObjectModel()[0] not viewObjectModel[0]
    – Joe
    Commented Jun 30, 2011 at 16:08
  • 1
    Thanks Joe, I knew it would be something obvious - it's been a long day ;-)
    – Alistair77
    Commented Jun 30, 2011 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.