Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assignDeep copy plain objects values instead of reference #151

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions reflections/shape/shape-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,13 @@ QUnit.test("assignDeepList", function(){
], "assigned right");
});

QUnit.test("assignDeep copy #150", function() {
var obj = {};
var objMap = {prop: { foo: 'bar' }};
shapeReflections.assignDeep(obj, objMap);
QUnit.notEqual(obj.prop, objMap.prop, "copy without referencing");
});


/*QUnit.test("getAllEnumerableKeys", function(){

Expand Down
14 changes: 10 additions & 4 deletions reflections/shape/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,18 +813,24 @@ shapeReflections = {
return target;
},
assignDeepMap: function(target, source) {

var hasOwnKey = fastHasOwnKey(target);
var getKeyValue = target[getKeyValueSymbol] || shiftedGetKeyValue;
var setKeyValue = target[setKeyValueSymbol] || shiftedSetKeyValue;

shapeReflections.eachKey(source, function(newVal, key){
if(!hasOwnKey(key)) {
// set no matter what
getSetReflections.setKeyValue(target, key, newVal);
// Plain objects needs to be serialized to make sure
// newVal is copied not referenced #150
if (typeReflections.isMapLike(newVal) && !typeReflections.isObservableLike(newVal)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't solve the original issue since it was using a DefineMap. Is that being handled somewhere else?

getSetReflections.setKeyValue(target, key, shapeReflections.serialize(newVal));
} else {
// set no matter what
getSetReflections.setKeyValue(target, key, newVal);
}
} else {
var curVal = getKeyValue.call(target, key);

// if either was primitive, no recursive update possible
if(newVal === curVal) {
// do nothing
Expand Down