Hi everyone,
I'm having a bit of a hard time with the function
periodically_call_remote. Here is my problem:
I have a periodically_call_remote in a partial. With ajax, I do a
replace_html of this partial for another partial, view/edit type of
thing. The 2nd partial doesn't contain a periodically_call_remote.
The problem is that the browser CONTINUES to query the server even tho
the periodically_call_remote shouldn't be there anymore, because the
partial changed.
Does anyone know how to disable this behavior? Can I call something in
my .rjs templates to disable/enablethe periodically_call_remote?
Thanks,
Jd
I hate replying to myself, but I just noticed somethign else. When I
come back from my partial that does not have a periodically_call_remote
to the one that has, the browser now queries the server TWICE. Once
for the original periodically_call_remote, and once for everytime the
partial is reloaded.
This is not good...
Hey JD-
You can setup a little switch you can use to turn of a periodical call like that. Just replacing the html does not stop the js from executing. You have to use a condition variable for that. Like this:
In the view partial to start the periodical call:
<div id='periodic'>
<script>stop_polling = false;</script>
<%= periodically_call_remote(:url => {:action => 'update_results'},
:frequency => 5,
:condition => "stop_polling == false") %>
</div>
And in the controller to handle the update and turn off the periodic call:
def update_results
render :update do |page|
page['periodic'].replace_html :partial => 'results_table'
page.assign 'stop_polling', true #<< here is the magick!
end
end
Cheers-
-Ezra
Ahhh, exactly what I was looking for... Thank you very much
But Unfortunately I have another question
I do a periodically_call_remote with a timestamp, to see if the item
was modified ever since the last readout from the database.
What would be the best way to update the timestamp that
periodically_call_remove is called with when I update an item?
<script>stop_polling = false;</script>
<%= periodically_call_remote :url => { :action =>
:check_for_modifications, :id => @item, :readout_from_db =>
Time.now.to_i},
:frequency => VERIFICATION_DELAY,
:condition => "stop_polling == false" %>
It is even possible ordo I have to refresh the entire page?
Thanks,
Jd