Closed jguillod closed 12 years ago
Jeremy,
I just hit this same issue. I had a model's set
event never get triggered because of shallow cloning. Why not use deep cloning in Model
's change
method?
Thanks,
--Martin
There's an issue at Underscore.js about that (https://github.com/documentcloud/underscore/issues/162), it seems like they don't want to add support foor deep cloning because there's no real reliable way of doing that.
Yep -- deep cloning isn't well supported or (often) desirable in JS. Backbone attributes are intentionally shallow ... and if you're using them to reflect your database columns and rows, things should line up nicely.
Hi, Consider the code below. When the attributes model have more than 1 level then changing the value of some deep level attributes of the clone (or the original) will be also changed in the original (or the clone) as they share the same deep-level attribute object. A
deepClone()
function could be not simply enhancement but bug fix because browsing through the code we can suspect that theclone()
should be replaced by thedeepClone()
in other places it is used. For instance this code: this.previousAttributes = .clone(this.attributes) with the current shallow cloning results in unexpected behavior when attributes are not a single level object. If you change a deep attributes likethis.attributes.address
thenthis._previousAttributes.address
points to the same object and will not report anymore the previous attributes state. Maybe you could think about a_.extendDeep()
like in jQuery.I survey this interesting library looking for some lightweight MVC and raise some questions. Is there some forum where to exchange about Backbone and underscore?
Thanks for the nice job. Joel