Gaurav Kumar
Gaurav Kumar

Reputation: 1091

create a copy of backbone model

I want to create a copy of a backbone model.

In javascript, the objects are passed by reference. so if i have a backbone model model1 and another as model2 then if i write

model2 = model1

then model2 also refer to same model as model1. Any changes i make in model2 or model1 will reflect in both models.

How can i achieve a case, where model2 is exact copy of model1 but do not refer to same model. So changes made in each model remain with that particular model only .

Upvotes: 1

Views: 241

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77482

You can use clone method

var model2 = model1.clone();

also use can use $.extend that allows you to simply copy object properties from one to another

Upvotes: 2

Related Questions