views:

14

answers:

1

Hey guys, I am trying to run a proccess by submitting an AJAX form:

<% form_remote_tag :url => {:controller => 'sender', :action => 'send'} do %>
<input id="messsageinput" type="text" size="44" name="command"/>
<%= submit_tag('Send', :id => 'button') %>
<% end %>

Then, the 'send' action creates a new BackgrounDRB job and does some render :update magic:

MiddleMan.worker(:transmit_worker).async_transmit(:arg => {:linkes => linkes, :command => params[:command], :userid => current_user.id}, :job_key => current_user.id)
    render :update do |page|
        page.insert_html :bottom, 'ajaxholder', :partial => 'transmit'
        page[:cominput].clear
    end

the part where I'm stuck is I am trying to somehow have the page check a 'getstatus' action that checks the progress of the job and renders appropriately and then stop checking when the job is reported done. Any suggestions please?

A: 

Alright, I found a solution. I had to use a bit of javascript to check if a variable is set true every 2 seconds and if it is run the AJAX updater (sort of like a periodical but with a variable on/off switch:

var updateInterval = 2000;
var timer;
var checkstatus = 0;
function inits()
{
<%= remote_function(:update => "feed",
      :url => { :controller => 'feed', :action => 'getfeed' }) %>;
timer = setInterval('checkel()', updateInterval);
}
function checkel(){
if(checkstatus=="1")
{
<%= remote_function(:url => { :controller => 'transmit', :action => 'getstatus' }) %>;
}
}
//-->

Then use a body onload="inits()" to initiate the script. The controller you pass the command to execute the worker should assign checkstatus a value of 1:

render :update do |page|
page.assign 'checkstatus', 1
end

Finally, in the status checking method, set it to reset the variable when completed to turn of the periodical check:

render :update do |page|
page.assign 'checkstatus', 0
page.replace_html 'successdiv', 'SUCCESS!'
end
hmind