1

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);
            });
        }
    }
}
Community
  • 1
  • 1
altschuler
  • 3,694
  • 2
  • 28
  • 55
  • do you need nesting with mapping plugin similar to this one - http://stackoverflow.com/questions/9951521/map-json-data-to-knockout-observablearray-with-specific-view-model-type/9951903#9951903 ? – Artem Jun 13 '12 at 23:40
  • 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 Jun 13 '12 at 23:55

1 Answers1

2

Solution was this binding

var mappings = {
    'Items': {
        create: function (options) {
            return new Item(options.data);
        }
    }
};
altschuler
  • 3,694
  • 2
  • 28
  • 55