1

I have an object like that

var users = [new User(),new User()];

Where inside I have another object like that

var location = new Location();

Then on my View I would bind my users array and when I bind the Location object I wouldn't type "Location.MyProperty1", "Location.MyProperty2" foreach bind but I would type just "MyProperty1", "MyProperty2"

it's not a bug, it's just a question. Can I do a binding like that using knockout?

3
  • how are you binding location i mean that are you using foreach binding for location? Commented Mar 11, 2014 at 15:36
  • no, I doing foreach for users Commented Mar 11, 2014 at 15:38
  • @xdumaine how you can see wasn't so hard to answer Commented Mar 11, 2014 at 16:40

1 Answer 1

2

There is the "with" binding, which creates a new binding context from an object:

<h1 data-bind="text: city"> </h1>
<p data-bind="with: coords">
    Latitude: <span data-bind="text: latitude"> </span>,
    Longitude: <span data-bind="text: longitude"> </span>
</p>

<script type="text/javascript">
    ko.applyBindings({
        city: "London",
        coords: {
            latitude:  51.5001524,
            longitude: -0.1262362
        }
    });
</script>

Taken from knockout documentation

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

Comments

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.