How to prevent a javascript/backbone.js cloned model from sharing attributes
Posted
by
user540727
on Stack Overflow
See other posts from Stack Overflow
or by user540727
Published on 2010-12-22T03:50:59Z
Indexed on
2010/12/22
3:54 UTC
Read the original article
Hit count: 471
I'm working with backbone.js models, so I don't know if my question is particular to the way backbone handles cloning or if it applies to javascript in general. Basically, I need to clone a model which has an attribute property assigned an object. The problem is that when I update the parent or clone's attribute, the other model is also updated. Here is a quick example:
var A = Backbone.Model.extend({});
var a = new A({'test': {'some': 'crap'}});
var b = a.clone();
a.get('test')['some'] = 'thing';
// I could also use a.set() to set the attribute with the same result
console.log(JSON.stringify(a))
console.log(JSON.stringify(b))
which logs the following:
{"test":{"some":"thing"}}
{"test":{"some":"thing"}}
I would prefer to clone a such that b won't be referencing any of its attributes. Any help would be appreciated.
© Stack Overflow or respective owner