Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a deep copy of a knockout object that was created by the mapping plugin

Tags:

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 in the hierarchy I want to put an Add button to insert a new blank copy of that element in the observablearray. The problem is I can't just say whateverArray.push(new MyObject()).

Since the mapping plugin actually created the whole hierarchy for me, I don't have access to "MyObject". So it seems the only thing I can do to insert a new item is to look at a previous item and copy it. I tried the ko.utils.extend function, but that doesn't appear to be making an actual clone. It gives me an object back, but when I update that object it still affects the original object that it was copied from.

See jsfiddle example

like image 827
emirhosseini Avatar asked Sep 08 '12 19:09

emirhosseini


People also ask

How do you create a deep clone of an object?

Copy an Object With Object.assign() was the most popular way to deep copy an object. Object. assign() will copy everything into the new object, including any functions. Mutating the copied object also doesn't affect the original object.

What is the most efficient way to deep clone an object in JavaScript?

According to the benchmark test, the fastest way to deep clone an object in javascript is to use lodash deep clone function since Object. assign supports only shallow copy.

What is deep cloning of object?

A deep copy of an object is a copy whose properties do not share the same references (point to the same underlying values) as those of the source object from which the copy was made.

How do you use a deep copy spread operator?

The spread operator makes deep copies of data if the data is not nested. When you have nested data in an array or object the spread operator will create a deep copy of the top most data and a shallow copy of the nested data.


2 Answers

There might be a way to set this up in the mapping settings but I can't quite figure that out just yet.

In the mean time, you could just unmap the object and map it back so you are essentially making a copy.

var newJob = ko.mapping.fromJS(ko.mapping.toJS(job)); 

This will be the easiest way to do it just like any other library, "deserialize" and "serialize" back again.


I was looking hard for a nice way to do this using the mapping options and found a way.

By default, the mapping plugin will take the observable instances from the source object and use the same instance in the target object. So in effect, both instances will share the same observables (bug?). What we needed to do was create a new observable for each property and copy the values over.

Fortunately, there is a handy utility function to map out each of the properties of an object. We can then create our new observable instances initialized with copies of the values.

// Deep copy var options = {     create: function (options) {         // map each of the properties         return ko.mapping.visitModel(options.data, function (value) {             // create new instances of observables initialized to the same value             if (ko.isObservable(value)) { // may want to handle more cases                 return ko.observable(value);             }             return value;         });     } }; var newJob = ko.mapping.fromJS(job, options); 

Note that this will be a shallow copy, you'll probably have to recursively map the objects if you want a deep copy. This will fix the problem in your example however.

like image 139
Jeff Mercado Avatar answered Oct 03 '22 20:10

Jeff Mercado


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.

like image 33
Teddy Avatar answered Oct 03 '22 19:10

Teddy