将 express-handlebars 与 Cytoscape.js 结合使用

Using express-handlebars with Cytoscape.js

嗨,我是网络开发的新手,我正在尝试使用 Cytoscape.js。我决定使用 Node.js、Express 和 Express-handlebars 来 运行 我的网页。我想使用 Cytoscape.js 来显示图形,但是我不确定如何使用把手来获取对容器的引用以初始化 cytoscape 对象。 这是我的 main.handlebars 文件:

<!doctype html>
<html>
<head>
    <title>Hello Cytoscape</title>
</head>
<body>
   {{{body}}}
</body>
</html>

这是我的 home.handlebars 文件:

<div id="cy"></div>  

这是我的 .js 文件:

/**
 * 
 */
'use strict';

var express = require('express');
var app = express();

var cytoscape = require('cytoscape');

//layout defaults to main, located in the views layout folder
var handlebars = require('express-handlebars').create({defaultLayout:'main'});

app.use(express.static('public'));

//sets the template engine to use for the express object
//a template engine will implement the view part of the app
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');


//initialize the cytoscape object
var cy = cytoscape({

  //<----not sure how to do this----->
  container: document.getElementById('cy'), // container to render in

  elements: [ // list of graph elements to start with
          { // node a
                  data: { id: 'a' }
          },
          { // node b
                  data: { id: 'b' }
          },
          { // edge ab
                  data: { id: 'ab', source: 'a', target: 'b' }
          }
  ],

  style: [ // the stylesheet for the graph
          {
                  selector: 'node',
                  style: {
                          'background-color': '#666',
                          'label': 'data(id)'
                  }
          },

          {
          selector: 'edge',
                  style: {
                  'width': 3,
                  'line-color': '#ccc',
                  'target-arrow-color': '#ccc',
                  'target-arrow-shape': 'triangle'
                  }
          }
  ],

  layout: {
          name: 'grid',
          rows: 1
  },
  // initial viewport state:
  zoom: 1,
  pan: { x: 0, y: 0 },

  // interaction options:
  minZoom: 1e-50,
  maxZoom: 1e50,
  zoomingEnabled: true,
  userZoomingEnabled: true,
  panningEnabled: true,
  userPanningEnabled: true,
  boxSelectionEnabled: false,
  selectionType: 'single',
  touchTapThreshold: 8,
  desktopTapThreshold: 4,
  autolock: false,
  autoungrabify: false,
  autounselectify: false,
  // rendering options:
  headless: false,
  styleEnabled: true,
  hideEdgesOnViewport: false,
  hideLabelsOnViewport: false,
  textureOnViewport: false,
  motionBlur: false,
  motionBlurOpacity: 0.2,
  wheelSensitivity: 1,
  pixelRatio: 'auto'
});                    

app.get('/', function (req, res) {
   var context = {};    
   res.render('home', context); 
});

//listener all for unrecognized urls
//return 404 not found response
app.use(function(req,res){
res.status(404);
res.render('404');
});

//listener for errors generate on server
//return 500 response
app.use(function(err, req, res, next){
console.error(err.stack);
res.type('plain/text');
res.status(500);
res.render('500');
});


if (module === require.main) {
// [START server]
// Start the server
var server = app.listen(process.env.PORT || 8080, function () {
 var port = server.address().port;
 console.log('App listening on port %s', port);
});
// [END server]
}

module.exports = app;

所以我想我的问题是如何获取对 div 容器 id="cy" 的引用以使用 express-handlebars 初始化我的 Cytoscape.js 图表,在此先感谢任何帮助。

如果您在服务器端 运行 Cytoscape——正如您在示例中所做的那样——那么它不会 运行ning 并且不会在客户端显示。

除非您只在服务器端进行图形分析,否则您应该在客户端使用 Cytoscape。您的页面 (main.handlebars) 是所有客户端的驱动程序,因此请将您的 Cytoscape 代码放在那里。或者在页面中,使用 <script> 标记引用您的 Cytoscape 代码。