Ruby on Rails中Coffeescript中的全局变量

时间:2013-07-18 16:24:08

标签: ruby-on-rails coffeescript

我的rails应用程序中有这个代码。在这段代码中计算多边形线的面积,我想计算多边形线的总和并保存在一个全局变量中,最后显示出来。但我的全局(s_area)var不保存数据。我怎么能这样做?

window.s_area = 0

jQuery ->
  $('#track_color').minicolors()


gm_init = ->
  gm_center = new google.maps.LatLng()#remove center lat lng

  gm_map_type = google.maps.MapTypeId.HYBRID
  map_options = {
    zoom: 8,
    center: gm_center,
    mapTypeId: google.maps.MapTypeId.HYBRID
  }
  new google.maps.Map(@map_canvas,map_options);



load_track = (id,map, info, point_data, name_data, ayear) ->
  callback = (data) -> display_on_map(data,map, id, info, point_data, name_data, ayear)
  $.get '/tracksegments/'+id+'.json', {}, callback, 'json'


display_on_map = (data,map, id, info, point_data, name_data, ayear) ->
  decoded_path = google.maps.geometry.encoding.decodePath(data.polyline)
  if ayear == '1'
    color = '#00FA15'
  else if ayear == '2'
    color = '#FA0400'
  else if ayear == '3'
    color = '#0532FA'
  #alert color
  path_options = { path: decoded_path, strokeColor: color , strokeOpacity: 0.5, strokeWeight: 4}
  track_path = new google.maps.Polyline(path_options)
  gm_path = track_path.getPath()
  area = google.maps.geometry.spherical.computeArea(gm_path)
  area = Math.round(area, 2)
  $("#area-val" + id).append(area)
  $("#show-area-val" + id).append(area + ' متر مربع')
  window.s_area = window.s_area + area

  if state != 2
    myCenter=new google.maps.LatLng(point_data[0][0], point_data[0][1])
  else
    myCenter=new google.maps.LatLng(point_data[0], point_data[1])
  marker=new google.maps.Marker({position:myCenter, map:map});
  track_path.setMap(map)
  infowindow = new google.maps.InfoWindow({content:'' + '<strong>' + 'شناسه: ' + '</strong>' + '<a href="/tracksegments/'+ '' + id + '' +'/edit">' +''+ info + '' +'</a>' + '<br/>' + '<strong>' + 'نام زمین: ' + '</strong>' + name_data + '' + '<br/>' + '<strong>' + 'مساحت به متر مربع: ' + '</strong>' + area + ''})
  #infowindow.open(map,marker) 
  google.maps.event.addListener(marker, 'click', -> (infowindow.open(map,marker)))
  map.fitBounds(calc_bounds(track_path));

calc_bounds = (track_path) ->
  b = new google.maps.LatLngBounds()
  gm_path = track_path.getPath()
  path_length = gm_path.getLength()
  i = [0,(path_length/3).toFixed(0),(path_length/3).toFixed(0)*2]
  b.extend(gm_path.getAt(i[0]))
  b.extend(gm_path.getAt(i[1]))
  b.extend(gm_path.getAt(i[2]))


#$ ->
 # map = gm_init()
  #load_track(js_track_id2,map)

$ -> 

    if state == 2
      map = gm_init()
      load_track(js_track_id2,map,info_data, point_data, name_single, ayear_single)
    else
      map = gm_init()
      ages = {}
      ages = js_track_id
      #for l,v of ages
       # load_track(v,map,v)
      for i of ages
        load_track(js_track_id[i],map,info_data[i], point_data[i], name_data[i], ayear_data[i])

$ ->      
  $('#total').append('Sum of Area: '+s_area)

2 个答案:

答案 0 :(得分:0)

这个

$('#total').append('Sum of Area: '+s_area)

需要这样:

$('#total').append('Sum of Area: '+window.s_area)

阅读how coffeescript does its scope将解释它。

答案 1 :(得分:0)

所以window.s_areadisplay_on_map计算:

display_on_map = (data,map, id, info, point_data, name_data, ayear) ->
  #...
  window.s_area = window.s_area + area

display_on_map在AJAX调用的成功回调中被调用:

load_track = (id,map, info, point_data, name_data, ayear) ->
  callback = (data) -> display_on_map(data,map, id, info, point_data, name_data, ayear)
  $.get '/tracksegments/'+id+'.json', {}, callback, 'json'

并且您在文档就绪处理程序中使用s_area

$ ->      
  $('#total').append('Sum of Area: '+s_area)

在触发文档就绪处理程序之前,$.get AJAX调用不太可能完成,因此当您尝试显示它时,s_area仍为零。

您可以在display_on_map中更新总计,如下所示:

window.s_area = window.s_area + area
$('#total').html("Sum of Area: #{window.s_area}")

至少应该以正确的顺序让事情发生。


PS:

  • 您可以说window.s_area += areas_area += area
  • 您可以使用字符串插值:$.get "/tracksegments/#{id}.json", ..."Sum of Area: #{s_area}"