4

I'm close to going nuts about this now and figured it'd be simple, however, there must be something I'm missing...

I've got a multidimensional array in a ko.observableArray, in which is so:

Site -> Company -> Job

For my data binds, data-bind="text: Site().Name" is fine, as you would expect. However, I can't access the sub-arrays by data-bind="text: Site().Company().Name" or data-bind="text: Site().Company.Name".

Has anyone else had the same issue, or is there something I'm doing drastically wrong? The objects are 100% being loaded into the array properly as I can see them in the console.

1 Answer 1

9

If I understand your problem correctly and a Site contains an observableArray of Company objects and each Company object contains an observableArray of Job objects, then your approach will not work.

data-bind="text: Site().Company().Name" is trying to get the Name property of an observableArray that happens to contain Company objects. You could however write data-bind="text: Site().Company()[0].Name" to get the name of the first Company.

A more common approach would be to iterate through the items. Something like:

<!-- ko with: Site -->
Site name is <span data-bind="text: Name"/>
<ul>
  <!-- ko foreach: Company -->
    <li>Company name is <span data-bind="text: Name"/>
      <!-- ko foreach: Job -->
        <li>Job name is <span data-bind="text: Name"/>
        </li>
      <!-- /ko -->
    </li>
  <!-- /ko -->
</ul>
<!-- /ko -->

See http://knockoutjs.com/documentation/foreach-binding.html for more details.

Hope this is what you are looking for and apologies if I have misunderstood your question.

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

1 Comment

Thank you for this, I'm going to go down the route of the foreach bindings (even if there's only 1 element in my arrays) as it just seems to make life easier.

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.