Looking for the most effective approach to breaking down a large data object retrieved from AJAX. When sending just one part (like paths), I typically use JSON.parse(data)
. However, my goal is to split the object into individual blocks first and then parse something like JSON.parse(data['paths'])
.
Below is a snippet of the JSON data:
{
"paths": {
"type": "FeatureCollection",
"features": [{
// Path feature details
}]
},
"beds": {
"type": "FeatureCollection",
"features": [{
// Beds feature details
}]
}
}
Sample JavaScript code:
$.ajax
dataType: 'text'
url: 'map.json'
success: (data) ->
Rails code generating the call:
data = { buildings: @geopaths, lawns: @geobeds }
respond_to do |format|
format.json { render json: data }
format.html
end
UPDATE: I initially refrained from explaining my requirements in detail as it might complicate matters. Simply put - I gather data from a database and send it to JavaScript for mapping various layers. Each layer, such as paths or beds, is encoded as GeoJSON in Rails before being transmitted via AJAX commands to JavaScript. While handling single layers poses no issue, I now need to incorporate multiple layers on the map. Given that AJAX typically handles a single object, I merge path and bed data into one object. My challenge lies in separating and utilizing these different portions of the object dynamically.
Here is my graphical representation:
paths = bunch of GeoJSON data
beds = bunch of GeoJSON data
Use AJAX to transmit paths and beds to JavaScript
Create pathMarkers using JSON.parse for the paths data
Create bedMarkers using JSON.parse for the beds data
If required, I can create a sample and share it on bitbucket for better understanding.