Event that signifies the tree is expanded/collapsed fully


I have a very basic functionality to expand and collapse the tree of a particular node for which I have the following code.

node = diagramInstace.findNodeForKey(nodeId);
if ( node.isTreeExpanded ) 


But the problems here is the scrollToPart is not working as expected.

I have tree layout applied by default on diagram.

Upon debugging it looked like my scrollToPart statement is executing before the tree is completely expanded or collapsed.

I tried TreeCollapsed and TreeExpanded event on diagram but still no luck.
Following is my code in these event

var it = arguments[0].subject.iterator;
while (it.next()) {
   var item = it.value;
   node = this._diagramInstace.findNodeForKey(item.data.id);

So is there a event which states the expand/ collapse process is completed and I can scrollToPart now.

I just tried this code in a sample that happens to have a tree in it:

    function test() {
      var nodes = new go.List(myDiagram.nodes).toArray();
      var node = nodes[Math.floor(Math.random() * nodes.length)];
      if (node) {
        if (node.isTreeExpanded) {
        } else {

Calling test() repeatedly seems to work well. Each time the diagram would be scrolled so that the selected and expanded-or-collapsed node was visible in the viewport.

Precisely which version of GoJS are you using? I’m using 2.1.9, but I have also tried 2.0.21 successfully.

To be precise I am running the above code on GoJS v1.8.33

and one small update though if I run the last code line as the following code then it works as expected.

    setTimeout(function() {


Yes, it was a new feature in v2.0 to have the CommandHandler.scrollToPart command automatically expand trees and groups if needed.

So it should work if I don’t use CommandHandler and directly use diagram.scrollToPart. correct ?