DiagramListener 没有被调用?

DiagramListener not being called?

我正在尝试捕获 TreeExpanded 和 TreeCollapsed 事件,但没有调用相应的侦听器。我已经按如下方式实现了监听器:

  myDiagram.addDiagramListener("TreeExpanded", function(e) {
    console.log(">>>>> STUFF HAPPENED in TreeExpanded");

  });

  myDiagram.addDiagramListener("TreeCollapsed", function(e) {
    console.log(">>>>> STUFF HAPPENED in TreeCollapsed");
  });

myDiagram 的完整代码如下所示:

  myDiagram = $(go.Diagram, "myDiagramDiv",
    {
      "undoManager.isEnabled": true,
      layout: $(go.TreeLayout),
      "ChangedSelection": onSelectionChanged
    });

  function geoFunc(geoname) {
    var geo = icons[geoname];
    if (geo === undefined) geo = icons["heart"];  // use this for an unknown icon name
    if (typeof geo === "string") {
      geo = icons[geoname] = go.Geometry.parse(geo, true);  // fill each geometry
    }
    return geo;
  }

  myDiagram.nodeTemplate =
    $(go.Node, "Auto",
      {isTreeExpanded:false},
      {doubleClick: function(e, node) {node.expandTree(1);}},
      $(go.Shape, "Rectangle",
        { strokeWidth: 2, stroke: colors["gray"],  },
        new go.Binding("fill", "color")),
      $(go.TextBlock, {stroke:"black",font:"12pt sans-serif",margin:3,wrap: go.TextBlock.WrapDesiredSize},new go.Binding("text", "geo"))


    );

  // Define a Link template that routes orthogonally, with no arrowhead
  myDiagram.linkTemplate =
    $(go.Link,
      { routing: go.Link.Orthogonal, corner: 5, toShortLength: -2, fromShortLength: -2 },
      $(go.Shape, { strokeWidth: 2, stroke: colors["gray"] })); // the link shape


  myDiagram.addDiagramListener("TreeExpanded", function(e) {
    console.log(">>>>> STUFF HAPPENED in TreeExpanded");

  });

  myDiagram.addDiagramListener("TreeCollapsed", function(e) {
    console.log(">>>>> STUFF HAPPENED in TreeCollapsed");
  });


  // Create the model data that will be represented by Nodes and Links
  myDiagram.model = new go.GraphLinksModel(
    [

如何检测节点树何时展开或折叠,然后调用函数?

调用Node.collapseTreeNode.expandTree是比较低级的操作。您的 doubleClick 事件处理程序不会执行事务(但任何更改都应该),也不会引发任何事件,例如 "TreeExpanded" DiagramEvent .

改为调用 CommandHandler.expandTreeCommandHandler.collapseTree。例如:

  {
    doubleClick: function(e, node) {
      e.diagram.commandHandler.expandTree(node); 
    }
  },