score:2

Accepted answer

This seems to be more of a JavaScript scope issue. Is tree_data already exposed to other functions (e.g. in the jQuery event handler, can you console.log(tree_data)? If so, then we just need an external reference to update.

A quick way is to assign it to the global window scope:

window.update = function(source) { ...

Of course this isn's not optimal, but it does help us narrow down your issue.

If that works, then we just need to declare a variable outside the scope of your build: function(tree_data) and then inside, on line 65, assign it to update:

var externalReferenceToUpdate;

[...]

build: function(tree_data) {
[...]
   var update = function(source) {
   [...]
   };
   externalReferenceToUpdate = update;

   update(root);  // moved this due to function expression vs. declaration

   [...]
}

As you noticed, we had to move update(root). This is because we've "made" the function using a different method. There's a whole other Stack Overflow discussion on the ways to declare functions and their scope issues. Essentially with the second way (var name = function()), the function is not "hoisted" to the top of its scope, so we had to call update after we assigned it to a variable.

Hope this helps!

PS In that other discussion, skim over the first answer and actually focus on the second answer by Eugene.


Related Query