I've got a horrible piece of jQuery code in one of my Rails views that listens for an event that looks something like this:
<% user_request_file = params[:file] == document.id.to_s %>
<% if user_request_file %>
<script type="text/javascript">
$(document).ready(function() {
$("#generate_file_btn_for_<%= document.id %>").attr('disabled', 'disabled').attr("href", "#").attr('title', 'Loading file...');
var doc_modal_<%= document.id %> = setInterval(function() {
$.getJSON( "<%= file_xhr_document_path(document) %>", function(file) {
if (file.file_generated) {
$("#loading_for_<%= document.id %>").fadeOut( "slow");
$('#document_file_modal_<%= document.id %>').modal('show');
$("#file_url_download_link_<%= document.id %>").attr("href", file.file_url);
clearInterval(doc_modal_<%= document.id %>);
} else if (file.file_error != null) {
window.location.href = "<%= file_error_document_path(document) %>";
}
});
}, 2000);
});
</script>
<% end %>
I want to replace it with something more modular and refactor-friendly. The code above listens for a request sent to its server every two seconds, and if it has been sent the view responds with information from that request.
Flight has been recommended to me as a suitable replacement framework but I'm really not sure how to begin to refactor it. Any advice or even recommendations other frameworks would be great.