0

I am on a project attempting to render search filter facets for a given query, but the rules are such that I cannot simply use nested ko: foreachstatements, as different result sets have different display rules for them.

The object I am mapping looks as follows:

FacetResults: ko.observableArray([
{
   Term: 'myTerm',
   Values: [{'key1': 5}, {'key2': 13}]
},
{  Term: 'myTerm2',
   Values: [{'key3': 6}, {'key4': 42}]
}]);

Basically, what I am trying to accomplish is something like

<!-- ko foreach: FacetResults()[0] -->
   <span data-bind="text: Term"></span>
   <ul data-bind="foreach: Values">
    <!-- do something -->
   </ul>
<!-- /ko -->

But am unable to do this without getting the 'unable to parse bindings' error.

I am able to do something like

<span data-bind="text: FacetResults[0]"></span>

and return 'Object object', but once I do <span data-bind="text: FacetResults[0[].Term" </span>, I get the same broken bindings error.

I have given thought to just breaking the result sets up into a number of observableArrays to avoid this, but are a use cases that do not require special presentation logic where I can make use of nested ko: foreach loops, and so I would like to stick to one array if possible (especially considering the data types returning will always be the same and save me from parsing these when they return from the call).

For what it's worth, I'm also able to inspect the objects at runtime, and have asserted that FacetResults[0].Term is a valid field through console.log(vm.FacetResults()[0].Term); without issue.

Any help will be appreciated. Thanks!

1 Answer 1

2

I believe you're looking for the with binding.

The with binding creates a new binding context, so that descendant elements are bound in the context of a specified object.

This should work for you

<!-- ko with: FacetResults()[0] -->
   <span data-bind="text: Term"></span>
   <ul data-bind="foreach: Values">
    <!-- do something -->
   </ul>
<!-- /ko -->
Sign up to request clarification or add additional context in comments.

1 Comment

yep, that was exactly it. Instant facepalm, thanks!

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.