It turns out a little puzzle in which the observed array with a knockout is divided into several viewing models.
Basically, I have a layout as follows
Transport ... textbox fields, etc Selected Passengers: <input type="checkbox" /> <button>Add Transport</button> Holiday ... textbox fields, etc Selected Passengers: <input type="checkbox" /> <button>Add Holiday</button>
Now the selected passengers for each section are generated from ONE observed array, the idea is that the passenger is deleted / changed, everything should automatically fall into place.
So something like this
function page() { // in actuality this passengers array is a computed observable obtained from the passengers section which is not shown here. this.allPassengers = ko.observableArray([ { Id: 1, name = ko.observable('name'), checked = ko.observable(false) }, { . . ]); } function transport() { // pageVM is a page object this.allPassengers = pageVM.allPassengers; this.transportItems = ko.observableArray(); this.addTransport = function() { this.transportItems.push({ . . selectedPassengers: [...] . . }); }; } function holiday() { // pageVM is a page object this.allPassengers = pageVM.allPassengers; this.holidayItems = ko.observableArray(); this.addHoliday = function() { this.holidayItems.push({ . . selectedPassengers: [...] . . }); }; }
However, when they click on add transport / vacation, I need a way to determine which checkboxes are checked so that I can add the selected passengers.
I tried to add the checked = ko.observable(false) property to the passenger element in parent.allPassengers , but the problem with this approach is that the checkbox is checked in the transport section, it will also check it in the holiday section, since it is using the same observable array.
Any ideas?
Edit:
violin example
Umair source share