0

I have this error

Unable to parse bindings.
Message: TypeError: Cannot read property 'length' of undefined;
Bindings value: text:SomeArray().length

I have recreated the error in here.

http://jsfiddle.net/LkqTU/12758/

var somevalue = [{Test:[]}]; // this is not always blank, but i cannot control it.
var ViewModel = function() {
     this.SomeArray=ko.observableArray(somevalue.Test);   
};

<span data-bind="text:SomeArray().length"></span>
<!-- ko if: SomeArray().length>0 -->
Hello World!
<!-- /ko -->

using

<!-- ko if: SomeArray() -->
Hello World!
<!-- /ko -->

doesn't creates the error but also doesn't skip the "Hello World" Message when the array is blank. i need to check if the array is blank and display the message only if not blank.

I can't use foreach because i am not looping though the array but just checking if it is blank only.

3
  • somevalue is an array! Your assigment in your viewmodel is wrong: it should be somevalue[0].Test jsfiddle.net/CB5Tz Commented Nov 29, 2013 at 16:21
  • oh. Then are there any other methods than length ko check for empty array? Commented Nov 29, 2013 at 16:35
  • length is perfectly fine for check array emptiness. But you initialize your SomeArray property with somevalue.Test which is undefined. So your SomeArray property does not contains an array therefore your SomeArray().length call throws an exception Commented Nov 29, 2013 at 16:44

1 Answer 1

1

somevalue is an array. So somevalue.Test is not defined but somevalue[0].Test will return [].

this.SomeArray=ko.observableArray(somevalue[0].Test);   

See fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

oh. Then are there any other methods than length ko check for empty array?
I don't understand your question. Please explain what you are trying to do ?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.