There are several items includes functions and observable variables in my ViewModel But at the time of posting, I need only the writable items.
var viewModel = {
firstName: ko.observable().extend({ minLength: 2, maxLength: 10 }),
lastName: ko.observable().extend({ required: true }),
emailAddress: ko.observable(),
age: ko.observable().extend({ min: 1, max: 100 }),
options: ['News', 'Music'],
subscription: ko.observable().extend({ required: true }),
password: ko.observable(),
submit: function () {
var model = cloneObservable(viewModel);
},
reset: function () {
}
};
ko.applyBindings(viewModel);
clone viewModel:
function cloneObservable(viewModel) {
var model = ko.observableArray([]);
Object.keys(viewModel).forEach(function (name) {
if (ko.isWritableObservable(viewModel[name])) {
model.push(viewModel[name]);
}
})
return ko.mapping.fromJS(ko.toJS(model));
}
viewModel[name] does not return Observable variable.
You have to get the instance of the viewModel and use that in
CloneObservablefunction.I removed
ko.mapping.fromJSfrom the function to illustrate that the values are coming correctly.