简体   繁体   English

coffeescript和流星变量范围问题

[英]coffeescript and meteor variable scoping issue

i'm using coffeescript with meteor and i think i'm having a problem with scoping. 我在流星上使用coffeescript,我认为范围有问题。

my app draws a floorplan and i'm trying to overlay a heatmap on top of it (using h337). 我的应用程序绘制了一个平面图,我正在尝试在其上覆盖一个热图(使用h337)。

@heatmap = null

initHeatMap = ( layer_name ) ->
  console.log 'creating heatmap on %s', layer_name
  h337.create
    container: document.querySelector layer_name
    gradient: 
      0.2: 'green'
      0.5: 'orange'
      0.8: 'red'
    radius: 11
    maxOpacity: 1.0
    minOpacity: 0.6
    blur: 0.6

drawHeatMap = ( metric ) ->
  @heatmap.setData
    max: 50
    min: 0
    data: regenData metric

redraw = ( metric ) ->
  console.log 'redraw: %o', @heatmap
  drawHeatMap metric 

Template.sensor_list.rendered = () ->
  @heatmap = initHeatMap '.heatmap'
  console.log 'created map: %o', @heatmap
  redraw 'temp'

basically, what i see from the chrome developer tools console is: 基本上,我从chrome开发人员工具控制台看到的是:

creating heatmap on .heatmap
created map: Heatmap
redraw: null

why is redraw() claiming that @heatmap is still null ? 为什么redraw()声称@heatmap仍然为null

coffeescript.org states that: coffeescript.org指出:

  • As a shortcut for this.property, you can use @property. 作为此属性的快捷方式,您可以使用@property。

Assuming this in coffeescript is like this in javascript, this is the function context, and will be different for each function. 在的CoffeeScript假设这样在JavaScript, 是函数上下文,并且将针对每个功能不同。 You never actually set the variable you created in global scope as it was always masked by the function context represented by the this object. 实际上,您永远不会在全局范围内设置您创建的变量,因为它始终被对象表示的函数上下文掩盖。

Try changing @heatmap (everywhere) to heatmap and see if that resolves it. 尝试将@heatmap(无处不在)更改为热图,看看是否可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM