Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Created observableArray is always empty

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);
like image 425
Alistair77 Avatar asked Jun 30 '11 16:06

Alistair77


1 Answers

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);
like image 143
Joe Avatar answered Oct 20 '22 15:10

Joe