0
votes

I would like to create a knockout custom binding (so I can add a computed observable) as with this example:

var mapping = {
    'children': {
        create: function(options) {
            return new myChildModel(options.data);
        }
    }
}

The problem is, my viewModel is the actual array (the root). Not a child property as 'children' in this example. So I need something like:

var mapping = {
    'root': {
        create: function(options) {
            return new myChildModel(options.data);
        }
    }
}

How can I achieve that? Thank you.

1

1 Answers

0
votes

Can't you just use the normal mapping function of the plugin to do that?

var viewmodel = ko.observableArray([]);
ko.utils.arrayForEach(options.data, function(data) {
    viewmodel.push(new myChildModel(data));
}

Though I would personally not make your view model the array, but make the array a property on the view model, it gives you more flexibility if you want to add more properties in the future.