Open jrwalt4 opened 8 years ago
a better deep copy algorithm to catch circular references
function flattenPrototype(p, depth, doNotInclude) {
depth = (depth !== void 0) ? depth || 5;
doNotInclude = (Array.isArray(doNotInclude)) ? doNotInclude || [];
if(doNotInclude.indexOf(p) < 0) {
doNotInclude.push(p);
}
var obj = (Array.isArray(p)) ? [] : Object.create(null);
for (var key in p) {
if (typeof p[key] === "object") {
// only add the object if we haven't reached max depth
if(depth > 0) {
// only add the object if it is not part of our doNotInclude list,
// which prevents circular references.
if(doNotInclude.indexOf(p[key]) < 0) {
obj[key] = flattenPrototype(p[key], depth - 1, doNotInclude.slice());
} else {
console.warn('circular ref:', p[key])
}
}
} else {
obj[key] = p[key];
}
}
return obj;
}
Use a function to deep copy values during acceptChanges.
Also, use
Object.create(null)
for DataRow._original