Reputation: 4940
I'm using google-maps-for-rails (gmap4rails). The gem has a method that created an array to use in javascript.
hash = Gmaps4rails.build_markers(@users) do |user, marker|
marker.lat user.latitude
marker.lng user.longitude
end
This results in the following:
[{:lat=>33.462209, :lng=>-86.812519, :infowindow=>"hello"}]
or simpler to read
[
{
:lat => 33.462209,
:lng => -86.812519,
:infowindow => "hello"
}
]
I am trying to use this in coffeescript, but where I pass this array into the coffeescript from a variable in my controller. (I am using Paloma JS for page-specific js, where you can access controller variables in your coffeescript.)
I've tried to convert the array into JSON and used gsub
to replace characters, but I've been having difficulty.
In specific, I tried to convert to JSON then remove the commas, hash.to_json.gsub(/,/, '')
, which outputs
[{"lat":33.462209"lng":-86.812519"infowindow":"hello"}]
I think I need to remove the brackets, {
and }
here, and possibly include the proper line indentions for coffeescript.
In the end, it should look like this:
[
lat: 33.462209
lng: -86.812519
infowindow: "hello"
]
Any ideas?? Thanks for the time.
Upvotes: 0
Views: 135
Reputation: 239312
I've tried to convert the array into JSON and used gsub to replace characters, but I've been having difficulty.
You should just use the JSON you're producing, as it is. It's completely valid CoffeeScript. There is no reason at all to try to strip anything out of it.
The CoffeeScript you're trying to produce is actually identical anyways. This...
[
lat: 33.462209
lng: -86.812519
infowindow: "hello"
]
is really [{ lat: ..., lng: ..., }]
, with the commas and {}
hidden but still syntactically present. CoffeeScript allows you to omit the {}
when defining an object literal, but it's still an object literal, and you gain nothing by stripping them out of your JSON. Either way, the structure you're producing will be identical: An array, containing one element, which is an object, containing properties lat/lng/infowindow.
Upvotes: 1