user1748217
user1748217

Reputation: 143

KnockoutJS Array

Please checkout this JS Bin http://jsbin.com/iqikuf/3/edit

When the below is run in the console, the result shown makes sense to me.

statement: vd.tools.push(dataItem);
resuelt: 2

But why do we get the below result when run in the console?

statement: vd.tools.length
result: 0

Upvotes: 2

Views: 158

Answers (2)

marcopeg
marcopeg

Reputation: 1238

I would like to integrate and explain DCoder's answer.

vd.tools refer to an observableArray which is a Javascript object which lengthproperty does not refer to the amount of stored items.

By "adding the parenthesis" vd.tools() we get a reference to the underlying array which is wrapped by the KnockoutJS's observableArray.

This is the real array which store our data and here we can ask for the length property expecting the correct result.

So then DCoder's answer make perfect sense.

Upvotes: 1

user1748217
user1748217

Reputation: 143

As DCoder pointed out

parentheses needed

vd.tools().length

Upvotes: 2

Related Questions