Here\'s my scenario. I\'m using the knockout mapping plugin to create an observable viewmodel hierarchy for me. My hierarchy has nested elements in it. At a particular point
ko.utils.clone = function (obj) {
var target = new obj.constructor();
for (var prop in obj) {
var propVal = obj[prop];
if (ko.isObservable(propVal)) {
var val = propVal();
if ($.type(val) == 'object') {
target[prop] = ko.utils.clone(val);
continue;
}
target[prop](val);
}
}
return target;
};
Here is my solution, hope it helps. In this code, obj
would be your viewModel object.