1
votes

I have a view model coming from the server as json like this

{
    Project: {
        Items: {
            ItemA: {
                Tags: [
                    ...
                ]
            },
            ItemB: ...
        }
    }
}

I'm then binding this object with the knockout.mapping plugin, but I need ItemA to have, for instance, an additional Marked observable, such that I in the markup could do something like

<ul data-bind="foreach: Project.Items">
    ...            
    <input type="checkbox" data-bind="checked: Marked">

I tried using the create option in the mapping process (as shown here ko.mapping create function, extend object), but I can't figure out how to nest the create method to extend the objects in Project.Items.

I've been trying mappings like this

var mappings = {
    'Items': {
        create: function (options) {
            return $.map(options.data, function(obj) {
                return new Item(obj);
            });
        }
    }
}
1
do you need nesting with mapping plugin similar to this one - stackoverflow.com/questions/9951521/… ? - Artem
yes, very much like it, but I need to override the creation of the objects in the Items property of Project, so I guess the question is how I override the creation of items in an observableArray - altschuler

1 Answers

2
votes

Solution was this binding

var mappings = {
    'Items': {
        create: function (options) {
            return new Item(options.data);
        }
    }
};