Get Diagram object from Electron MenuItem

I have an Electron app with GoJS. When I invoke a menu item in the Electron app, I want to modify my GoJS diagram, but I can’t figure out how to get a handle on the myDiagram object. How would I do that?

I assume you are talking about some HTML element acting as a menu item.

When establishing its click (or onclick) behavior, you could use a JavaScript closure that captures the value of myDiagram or however you are referring to the instance of Diagram.

I am referring to an instance of Diagram – basically, when a Electron menu item is invoked, it calls a javascript method. That method should call some methods on go.Diagram, but I can’t get a handle on my diagram object.

Here is my code, which does not work:

function getOpenFileMenuItem(){
    const go = require("gojs");
    const CQFileUtils = require("./cqfile_utils.js");  // requires module.exports assignment at end
    const { MenuItem, dialog } = require('electron')
    let myDiagram = go.Diagram.fromDiv("myDiagramDiv");
    return new MenuItem(
            id: '1',
            label: "Open CQ File",
            accelerator: "CommandOrControl+O",
                let selectedFiles = dialog.showOpenDialogSync(openFileDialogOptions)
                if (selectedFiles && selectedFiles.length > 0){
                    CQFileUtils.readAndLoadFile(myDiagram, selectedFiles[0]);

leads to -->

Uncaught Exception:
TypeError: Cannot read property 'getElementById' of undefined

If you have a reliable reference to the id of the HTMLDivElement associated with the Diagram, then using go.Diagram.fromDiv is a good idea. Or if you have a reference to that Div element, using go.Diagram.fromDiv is also a good idea.

Have you checked the value of myDiagram in your getOpenFileMenuItem function? If it’s null because it hadn’t been set up yet, or because the id isn’t valid any more, then that would explain the exception.