I am attempting to create an array of contract line items (CLINs) that will be displayed as individual div
elements below a header of general contract information.
I am able to get the normal observables to work, but it appears that the passing of the array via the constructor for the view model is not creating any part of the clins observable array.
I have a jsFiddle that illustrates my problem. What is strange to me is that the data-bind="text: clins.length()
on the HTML span tag does not even return zero, but instead renders nothing.
Is there anyway to enable debugging within a jsFiddle or should I see a warning/error?
For anyone who wants to get the length of an observableArray without the
_destroy
ed items, you can use this function:Then to use it:
TL;DR / Quick Fix
Execute the
observableArray
as a function to get the underlying array, then get its length:Why does this happen?
Let me provide an answer that provides some details in addition to @kendaleiv's solution, and that also answers a highly related question I feel many people have:
A typical repro of such a scenario would be this:
The snippet says "length = 0", instead of "length = 3".
The key thing to note here:
ko.observableArray
is a function... that returns a function.That's right: if you assign the result of it to a variable or member, such as
myObsArray
in the example, that variable will be a reference to a function. So if you're asking forthen you're actually asking for the
Function.length
property: the number of arguments that function takes. For a function returned fromko.observableArray
this will be 0 (zero).How do we fix this?
The fix given in other answers is fine, let me reiterate here with this answer's example. To get the length of "the array being observed", you need to invoke said function to get the array, and then get its length:
Finally, as a footnote, OP's code is something equivalent to:
This will find aforementioned Function.length property and try to execute it as a function (which it isn't), and will not return 0 but instead crash. Beware to not invoke
length
as a function, but the observable. Always invokelength
as a member variable.Will / Can / Could this be changed?
Very early on KO's developers considered this issue; as early as Github issue no 4. That issue explains the things I've tried to cover above too, and also shows that in the end there wasn't much that could be done to change it.
Errors from jsfiddle pages do get sent to your browser.
As for your error, try this:
This turns the
observableArray
into anarray
and uses the array'slength
property.See the updated the jsfiddle as well.