0

I'm trying to map some data (returned from a jQuery $.ajax call) to a view model, but for some reason the nested array is not mapped.

    <p>Name: <input data-bind="value: Name" /></p>
    <p>Age: <input data-bind="value: Age" /></p>
    <p>Friends: <span data-bind="text: Friends.length"></span></p>



    <script type="text/javascript">
        var viewModel = null;
        var data = { "Name": "Simon", "Age": "24", "Friends": [{ "Name": "Bill", "Age": "24" }, { "Name": "Peter", "Age": "21"}]};
        viewModel = ko.mapping.fromJS(data);
        ko.applyBindings(viewModel);
    </script>

In this simple example Simon has no friends...

1 Answer 1

1

mapping.fromJS converts arrays to observable arrays, so Friends is an observable array and to access its underlying javascript array you need to call it: Friends().length

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.