简体   繁体   English

Ruby on Rails中Coffeescript中的全局变量

[英]Global variable in Coffeescript in Ruby on Rails

I have this code in my rails application. 我的Rails应用程序中有此代码。 in this code calculate area of poly lines and i want to calculate sum of poly lines and save in a global variable and finally show that. 在这段代码中,计算折线的面积,我想计算折线的总和并保存在全局变量中,最后显示出来。 but my global (s_area) var does not save data. 但是我的全局(s_area)变量不保存数据。 how can I do this? 我怎样才能做到这一点?

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)

This 这个

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

needs to be this: 需要这样的:

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

Reading up on how coffeescript does its scope will explain it. 仔细阅读coffeescript的作用范围将对此进行解释。

So window.s_area is computed by display_on_map : 所以window.s_area是由display_on_map计算的:

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

and display_on_map is called in the success callback of an AJAX call: 并且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'

and you're using s_area in a document-ready handler: 并且您在准备文档的处理程序中使用s_area

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

It is unlikely that the $.get AJAX calls will complete before the document-ready handler is triggered so s_area is still zero when you try to display it. $.get AJAX调用不太可能在触发文档就绪处理程序之前完成,因此当您尝试显示它时s_area仍为零。

You could update the total as you calculate in display_on_map , something like this: 您可以在display_on_map计算总数,如下所示:

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

That at least should get things happening in the right order. 至少应该使事情以正确的顺序发生。


PS: PS:

  • You can say window.s_area += area or just s_area += area . 您可以说window.s_area += area或只是s_area += area
  • You can use string interpolation: $.get "/tracksegments/#{id}.json", ... and "Sum of Area: #{s_area}" . 您可以使用字符串插值: $.get "/tracksegments/#{id}.json", ... "Sum of Area: #{s_area}" $.get "/tracksegments/#{id}.json", ..."Sum of Area: #{s_area}"

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

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