admin管理员组文章数量:1405314
I need to clone a tree I made using TreeModel.js. What I exactly need to do is duplicating it, make changes to it and check if the number of nodes decreased. If it did, revert to the original tree. Here's a small example of what I do so far to duplicate it, which is not correct:
var tree = new TreeModel();
var root = tree.parse({
id: 0,
name: "Root",
children: [{id: 1, name: "1", children: []},{id: 2, name: "2", children: []}]
});
console.log(root)
var dup = tree.parse(root)
console.log(dup)
Here's a Fiddle. You'll see the difference between the trees by looking at the console:
Node {config: Object, model: Object, children: Array[2], isRoot: function, hasChildren: function…}
Node {config: Object, model: Node, children: Array[2], isRoot: function, hasChildren: function…}
Is there any way to properly clone such a structure? I looked for cloning JS object but still, I can't find a way for cloning this object exactly (such as the prototypes of properties like the model...)
I need to clone a tree I made using TreeModel.js. What I exactly need to do is duplicating it, make changes to it and check if the number of nodes decreased. If it did, revert to the original tree. Here's a small example of what I do so far to duplicate it, which is not correct:
var tree = new TreeModel();
var root = tree.parse({
id: 0,
name: "Root",
children: [{id: 1, name: "1", children: []},{id: 2, name: "2", children: []}]
});
console.log(root)
var dup = tree.parse(root)
console.log(dup)
Here's a Fiddle. You'll see the difference between the trees by looking at the console:
Node {config: Object, model: Object, children: Array[2], isRoot: function, hasChildren: function…}
Node {config: Object, model: Node, children: Array[2], isRoot: function, hasChildren: function…}
Is there any way to properly clone such a structure? I looked for cloning JS object but still, I can't find a way for cloning this object exactly (such as the prototypes of properties like the model...)
Share Improve this question asked Dec 15, 2014 at 9:58 JohyJohy 3171 gold badge5 silver badges17 bronze badges4 Answers
Reset to default 3You can deep clone the model of the first tree and parse it again to get a second tree.
Taking on your example:
function deepCopy(obj) {
// You can also use the jquery extend method here
return JSON.parse(JSON.stringify(obj));
}
var dup = tree.parse(deepCopy(root.model));
Important: If you do not deep clone the model, and just parse it again, you'll end up with the same underlying model shared by both trees which will certainly cause inconsistencies.
I finally came to a solution that may help anyone with the same problem:
var tree = new TreeModel();
var root = tree.parse({
id: 0,
name: "Root",
children: [{id: 1, name: "1", children: []},{id: 2, name: "2", children: []}]
});
console.log(root)
var dup = tree.parse(root.model)
console.log(dup)
The parse
function takes a model as parameter and the model of root seems to work fine.
EDIT: this solution may bring inconsistencies since the 2 trees are based on the same model. JNS's solution is more appropriate.
Why not trying a jQuery deep copy?
var dup = jQuery.extend(true, {}, tree)
I tried your fiddle but it doesn't seem to work.
https://github./mrluc/owl-deepcopy this worked for me.
newTree = deepCopy(tree)
本文标签: javascriptCloning a JS TreeModel treeStack Overflow
版权声明:本文标题:javascript - Cloning a JS TreeModel tree - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744333181a2601062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论