Created
April 23, 2012 23:02
-
-
Save gabriel-dehan/2474415 to your computer and use it in GitHub Desktop.
Recursively delete in an Object tree and retrieve deleted node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Tree = Base.extend({ | |
constructor: function(tree) { | |
this.tree = tree; | |
this.count = this.count_nodes(0); | |
}, | |
delete: function(node_id, tree){ | |
var that = this; | |
if ( tree === undefined ) tree = that.tree; | |
_.each(tree, function(node, i){ | |
if ( node_id == node._id ) { | |
tree.remove(i); | |
// IMA NEED TO RETURN node; | |
} | |
if ( node.is_dir ) | |
that.delete(node_id, node.tree); | |
}); | |
} | |
} | |
document_tree = new Tree(t); | |
deleted_node = document_tree.delete(1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var t = [ | |
{ | |
is_dir : 0, | |
name : 'file.txt', | |
_id : 0 | |
}, | |
{ | |
is_dir : 1, | |
name : 'foo', | |
_id : 1, | |
tree : [ | |
{ | |
is_dir : 0, | |
_id : 2, | |
name : 'leaf.txt' | |
} | |
] | |
}, | |
{ | |
is_dir : 1, | |
name : 'bar', | |
_id : 3, | |
tree : [ | |
{ | |
is_dir: 0, | |
_id : 4, | |
name : 'leaf1.txt' | |
}, | |
{ | |
is_dir: 0, | |
_id : 5, | |
name : 'leaf2.txt' | |
}, | |
{ | |
is_dir : 1, | |
name : 'bar', | |
_id : 6, | |
tree : [] | |
} | |
] | |
} | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment