I have two lists of data and you want to combine them into one list with a knockout display.
This seems like normal if I define a key to compare, except that it removes items not listed in the last update.
Is it possible to use KO Mapping for an array without deleting elements that are not found in the last list?
EG below should be done:
not
<ul data-bind='foreach: mergedList'> <li> <span data-bind='text: id'></span> <span data-bind='text: a'></span> <span data-bind='text: b'></span> </li> </ul>
var listA = [ { id: 1, a: 'A'}, { id: 2, a: 'AA'}, { id: 3, a: 'AAA'} ]; var listB = [ { id: 1, b: 'B'}, { id: 2, b: 'BB'}, { id: 4, b: 'BBB'} ]; var mapping = { key: function(data) { return ko.utils.unwrapObservable(data.id); }, create: function (options){ var model = new subViewModel(); ko.mapping.fromJS(options.data, {}, model); return model; } } var subViewModel = function(){ var self = this; self.a = ko.observable(); self.b = ko.observable(); } var viewModel = function(){ var self = this; self.mergedList = ko.observableArray(); } var vm = new viewModel(); ko.mapping.fromJS(listA, mapping, vm.mergedList); ko.mapping.fromJS(listB, mapping, vm.mergedList); ko.applyBindings(vm);
http://jsfiddle.net/BQRur/9/
source share