I’d like to be able to iterate over the knockoutJS observable properties of an object and copy them across to another object which holds other observable properties. The target object exists within an observableArray (in this case selected is the target and is set on an ‘edit’ button click).
‘Item’ is passed in the viewmodel which is bound to a dialog. If cancel is clicked, the item is just ignored, if ok is clicked, the data is persisted on the server, and then item properties are copied across to selected.
Currently I’m doing it like so:
function AttachCommitData(item) {
for ( var prop in item) {
if ( prop !== "undefined" ) {
vm.selected[item](item[prop]);
}
}
}
I’m not sure if this is the way to do it, but it throws an exception as the item holds an observableArray, and the copy to the observable fails. Is there anyway to only iterate over the obseravables and miss anything else in the loop such as observableArrays, functions etc?
If you could advise also, if this is the best way of attaching a set of observables to another set or not.
I would probably use mapping plugin for this:
See mapping plugin documentation for details.