Global variable in Coffeescript in Ruby on Rails -


i have code in rails application. in code calculate area of poly lines , want calculate sum of poly lines , save in global variable , show that. global (s_area) var not save data. how can 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()   = [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)       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) 

so window.s_area computed 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 called in success callback of ajax call:

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 document-ready handler:

$ ->         $('#total').append('sum of area: '+s_area) 

it unlikely $.get ajax calls complete before document-ready handler triggered s_area still 0 when try display it.

you update total calculate in display_on_map, this:

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

that @ least should things happening in right order.


ps:

  • you can window.s_area += area or s_area += area.
  • you can use string interpolation: $.get "/tracksegments/#{id}.json", ... , "sum of area: #{s_area}".

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -