Skip to content

Commit

Permalink
Bugfix: with the dotPathIsChildTree updates, objects passed to .set w…
Browse files Browse the repository at this point in the history
…ere being modified. This clones the object, eliminating modification
  • Loading branch information
relativityboy committed Mar 29, 2017
1 parent 76de10b commit a9cf895
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ define([

//1. collect arguments into a single format
if (typeof key === 'object') {
attrs = key;
attrs = _.clone(key);
options = val;
} else {
(attrs = {})[key] = val;
Expand Down
18 changes: 18 additions & 0 deletions test/base.model.dotpathischildtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ describe("Base.Model.dotPathIsChildTree=true - transform x.get/set('a.b.c') into
expect(aModel.get('b').get('c')).to.equal(cValue);
expect(aModel).to.not.equal(model.get('a'));
});
it("object passed to .set is not modified (side effects of that sort are bad!)",function(){
var attrs = {
'a.b.c':newValue,
'm':'pqr'
},
attrs2 = _.clone(attrs);
//make sure they're not the same object
expect(attrs).to.not.equal(attrs2);
//make sure they're logically equivalent.
expect(JSON.stringify(attrs)).to.equal(JSON.stringify(attrs2));
model.set(attrs);
//make sure the values got set (not needed but nice to be sure)
expect(model.get('a').get('b').get('c')).to.equal(newValue);
expect(model.get('m')).to.equal('pqr');

//and the money test. Are these two still logically equivalent?
expect(JSON.stringify(attrs)).to.equal(JSON.stringify(attrs2));
});
it("throws an error if attempting to set the child of a raw object",function(){
assert.throws(function() { model.set('x.y.z',newValue); }, Error);
});
Expand Down

0 comments on commit a9cf895

Please sign in to comment.