Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron 'window' is not defined

Tags:

electron

I'm trying to build an app that draws a line chart using the Google Charts api. One thing I've got set up is an event listener on window for the 'resize' event. This triggers the line chart to redraw with dimensions set to 'window.innerWidth' and 'window.innerHeight' to fit the new window size.

This all works just fine if I run the app using npm start. Problem is when I build the app using electron-packager. Running the exe it builds throws this:enter image description here

Here's my HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>AP Test Viewer</title>
    <link rel="stylesheet" href="css/styles.css">

  </head>
  <body ng-app="ApApp" ng-controller="MainController">

    <a ui-sref="single">Single</a>
    <a ui-sref="multi">Multi</a>

    <ui-view></ui-view>


  </body>

  <script>
    // You can also require other files to run in this process
    require('./renderer.js');
    require('./js/loader.js'); //google charts loader.js
    require('./js/app.js');
  </script>
</html>

and the relevant part of my app, where I use window is here:

app.fun.drawGraph = function(){
    if(app.data.graphConfig){
        app.data.graphConfig.options.width = window.innerWidth * 0.97;
        app.data.graphConfig.options.height = window.innerHeight * 0.90;

        var chart = new google.visualization.LineChart(document.getElementById('chart'));
        chart.draw(app.data.graphConfig.data, app.data.graphConfig.options);
        // chart.draw($scope.graphData, google.charts.Line.convertOptions(options));    
    }
}

window.addEventListener('resize', function(e){
    if(app.data.graphConfig){
        app.fun.drawGraph();
    }
})

As far as I know, this should just be 'window' as can be accessed by javascript on a webpage.

like image 604
Magic Marbles Avatar asked Nov 07 '16 23:11

Magic Marbles


1 Answers

It looks like the relevant Javascript you are referencing is running in the main process rather than the renderer processes. If you move the code that references window into a script tag in the html you shouldn't see the same issue.

As far as requiring the files from the html page I would expect it to work as if it were in a script tag also. I could be mistaken though.

like image 88
payne8 Avatar answered Oct 05 '22 21:10

payne8