如何在Meteor中通过多种方法访问Raphael论文

时间:2013-03-13 02:15:50

标签: raphael meteor

我正在使用Raphael与Meteor并遇到问题。我正在使用paper创建var paper = Raphael("paper", 800, 600);如果我将此代码放在渲染函数中,比如Template.board.rendered,我可以正常使用它。但是,即使我在模板中调用它们,其他函数也无法访问此模板中的纸张变量。我认为我可以通过在Meteor.startup中创建论文来解决这个问题,但我仍然遇到同样的问题。

例如,以下代码不起作用:

Pieces = new Meteor.Collection('pieces');

Meteor.startup(function () {
  var paper = Raphael("paper", 800, 600);
});

Template.board.rendered = function () {
  // If I create the paper in here along with the contents of createGoban
  // it works, but then I can't use it anywhere else...
  // var paper = Raphael("paper", 800, 600);

  createGoban();
};

var createGoban = function () {
  // Create goban
  // Based off of svg in public domain: http://commons.wikimedia.org/wiki/File:Blank_Go_board.svg
  // Converted using http://readysetraphael.com/
  var rect_a = paper.rect(0, 0, 96, 96);
  rect_a.attr({fill: '#DCB35C','stroke-width': '0','stroke-opacity': '1'}).data('id', 'rect_a');
  var path_b = paper.path("M2.9,93h90.2m-.2-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m0-5h90m0-5H3m-.1-5h90.2M3,3V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3m5,0V93m5,0V3");
  path_b.attr({stroke: '#000',"stroke-width": '0.2',fill: 'none','stroke-opacity': '1'}).data('id', 'path_b');
  var path_c = paper.path("M18,78l0,0m30,0l0,0m30,0l0,0m0-30l0,0m-30,0l0,0m-30,0l0,0m0-30l0,0m30,0l0,0m30,0l0,0");
  path_c.attr({stroke: '#000',"stroke-width": '4',"stroke-linecap": 'round','stroke-opacity': '1','fill': '#000000'}).data('id', 'path_c');
  var goban = [rect_a, path_b, path_c];

  // Scale goban up since rsr outputs reduced size
  for(var i = 0; i < goban.length; i++) {
    goban[i].transform('S5,5,0,0');
  };
};

1 个答案:

答案 0 :(得分:3)

删除var内的startup()后,您的代码正常运行,这使得paper全局:

Meteor.startup(function () {
  paper = Raphael("paper", 800, 600);
});