如何使用浏览器化脚本中传递给index.jade的对象?

时间:2019-03-09 18:28:48

标签: node.js browserify pug-loader

我正在尝试使用browserify并遇到我的项目结构问题。 我通过从index.js路由将数组发送到index.jade。这似乎可行,并且在index.jade中显示了数组的长度:

extends layout

block content
  h1= title 
  p= spectra.length
  div#cy
  script(src="javascripts/code.js")

Code.js是browserify打包的许多类(ES6被Babelified)。 在我尝试使用光谱之前,这似乎可行:

"use strict";
const GraphSpectrum = require("./GraphSpectrum");
let gms = new GraphSpectrum(spectra[0]); 
var cy = window.cy = cytoscape({ //...

ReferenceError:未定义光谱

browserify创建code.js,并在生成的代码的末尾包含我的客户代码:

......
},{}],4:[function(require,module,exports){
"use strict";

console.log(spectra.length);
console.log(spectra);

const GraphSpectrum = require("./GraphSpectrum");
const MSSpectrum = require("./MSSpectrum");

let mgfSection = ["BEGIN IONS", "PEPMASS=491.222686767578", "CHARGE=2", "TITLE=491.222686767578_1494.17_scan=6268_2014090922Mix2alkylISW10noEclu,seq={ATNYNAGDR},sup={4}", "SCANS=0", "491.2227\u00092", "128.1677\t34.3", "143.9659    14.8", "145.1864    1063.5", "147.2310  164.8", "148.0274   88.9", "152.2586    32.3", "153.1165    141.1", "155.0703   453.6", "156.2521   121.2", "158.0017   158.1", "162.1551   94.7", "163.1792    69.3"];
let msms = new MSSpectrum();
msms.parseMGFSection(mgfSection);
let gms = new GraphSpectrum(msms);
console.log(gms.nodes);
console.log(gms.getEdges('sequence'));
var cy = window.cy = cytoscape({
  container: document.getElementById('cy'),
  boxSelectionEnabled: false,
  autounselectify: true,
  style: [{
    selector: 'node',
    css: {
      'content': 'data(id)',
      'text-valign': 'center',
      'text-halign': 'center'
    }
  }, {
    selector: '$node > node',
    css: {
      'padding-top': '1px',
      'padding-left': '1px',
      'padding-bottom': '1px',
      'padding-right': '1px',
      'text-valign': 'top',
      'text-halign': 'center',
      'background-color': '#bbb'
    }
  }, {
    selector: 'edge',
    css: {
      'curve-style': 'bezier',
      'target-arrow-shape': 'triangle'
    }
  }, {
    selector: ':selected',
    css: {
      'background-color': 'black',
      'line-color': 'black',
      'target-arrow-color': 'black',
      'source-arrow-color': 'black'
    }
  }],
  elements: {
    nodes: gms.nodes,
    edges: gms.getEdges('sequence') 
  },
  layout: {
    name: 'preset',
    padding: 5
  }
}); 

},{"./GraphSpectrum":1,"./MSSpectrum":3}]},{},[4]);

该代码有效,并且当我删除光谱参考时显示结果。 如何访问此代码部分中pug中可用的全局变量?

我想念什么?

谢谢你, 詹斯

0 个答案:

没有答案