I have rails application that has two view ports that uses the same data. I can get the data in JSON format like this:
$.read(
'/en/search/fetch_companies',
{ current_location_lat: 72, current_location_lng: 10, bounding_box: 10 },
function (response) {
for (var i = 0; i < response.length; i++) {
var markerPosition = new google.maps.LatLng(response[i].latitude, response[i].longitude);
var marker = new google.maps.Marker({
position:markerPosition,
map:map,
title:"AJAX!"
});
}
}
);
This code above creates Markers on a Google Maps. The second view port is a simple table.
<% @companies.each do |company| %>
<tr>
<td>
<address>
<strong><%= company.name %></strong><br>
<%= company.address %>
<br>
<%= company.city %>,<%= company.province %>
<br>
<%= company.postalcode %>
</address>
</td>
<td>
<%= link_to book_appointment_path(:choose_service, :company_id => company.id), :class => "btn btn-info btn-center-col" do %>
<i class="icon-time icon-white"></i> Book Appointment
<% end %>
</td>
</tr>
<% end %>
For now I’ve been sending two requests to get the JSON data. One for the table view and one for the Map. This is not acceptable for me. The Map and the Table need to be fed the same data at once, but since Rails is resolved on the server I cannot fill the table with the JSON data.
Do anyone knows about some a table that plays nicely with Rails, Ajax and pagination?
A good enough answer for this one. I think it’s the cleanest solution given the parameters of this question.
Basically I create a RESTful service that allows me to search for something and I use the Backbone library to display my results. I requires more work then a simple Rails component since you need to do JS but to support AJAX correctly, it’s the way to go.