ruby-talk
4/2/2007 2:14:00 PM
Hi Matt,
As you suggested I put the "poll_message"-div + JS-code + periodically_...
inside a partial rendered at startup.
I also got rid of the RJS-template in exchange for your "respond_to" thing
But here is the thing:
my io_process-method looks sort of like this:
def io_method
session[:message] = "starting io..."
# ...
if (blah)
session[:message] = "finished"
else
session[:message] = "an error occurred!"
end
respond_to do |format|
format.js { render :update do |page|
page.insert_html :bottom, 'poll_message', :partial => 'message'
end
}
session[:message] = nil
end
my partial "_message.rhtml" just calls "<%= session[:message] %>"
So when the interpreter gets to "respond_to",
it only renders the last message defined by the io_method.
Also the message is rendered after io_method is done.
So I am actually where I started from:
the template messages get rendered after the controller method is done.
Thanks for your help!
Cheers,
Tom.
---------------------------------
On Mon, 2 Apr 2007 19:57:54 +0900
"Matthieu Stone" <matt.stone@cityticketexchange.com> wrote:
> Hi Tom,
>
> I think that this will answer your questions.
>
> To get rid of the firebug message, just make sure that this routine
>
> ><div id="poll_message"><%= session[:message] %></div>
> > <script type="text/javascript">
> > //<![CDATA[
> > <% if @session[:message] && !@session[:message].blank? %>
> > poll_message = true;
> > <% else %>
> > Poll_message = false;
> > <% end %>
> > //]]>
> > </script>
>
> Is rendered in the page before the periodically_call_update routine, it's
> easier to place this in a partial, then you can it via RJS in any controller
> method. I've added the poll_message div to make things a little more
> explicit.
>
> session[:message] = "let's get it started.."
>
> respond_to do |format|
> format.js { render :update do |page|
> page.replace 'poll_message', :partial =>
> 'poll_message'
> page.visual_effect :highlight, 'poll_message',
> :duration => 1,
> :startcolor => '"#FFCCFF"',
> :endcolor => '"#FFFFCC"'
> end
> }
> end
>
>
> I use the respond_to mechanism to update div's on the page, rather than .rjs
> files. I don't need to specify :update => some_div in the
> periodically_call_remote routine & I can do other things, such as highlight
> the updated message or update another part of the screen using logic in the
> controller. There are upsides & downsides to doing things this way, I just
> find it easier to do it in the controller as it keeps all my page logic in
> the one place.
>
> rgds,
> - matt.
>
> -----Original Message-----
> From: ruby-talk@hinv.org [mailto:ruby-talk@hinv.org]
> Sent: 02 April 2007 11:17
> To: ruby-talk ML
> Subject: Re: updating an RJS template _while_ executing a method
>
> Hey Matt,
>
> what I still don't understand is
> where to turn "poll_message" on and off using your JS-code?
> Or: what do you mean with "...in a RJS update..."?
>
> -> Inside the RJS-template of "my_io_process"?
> That throws quite a few errors.
>
> -> Using inline RJS rendering inside my controller?
> (which shouldn't work while also using an RJS template)
>
> -> somewhere else..?
>
> Also:
> Do I need to specify a ":update => 'my_div'"
> within periodically_call_remote?
> When I put this call inside my layout,
> Firebug yells at me: "poll_message not defined"...
> Is a "var poll_message = false" fine?
>
> Thanks for your help!
> Cheers,
> Tom.
>
> ---------------------------------
> On Sun, 1 Apr 2007 04:43:45 +0900
> "Matthieu Stone" <matt.stone@cityticketexchange.com> wrote:
>
> > Here's one way
> >
> > In your method doing the IO stuff, update a session variable with the
> > message that you want displayed; ie:
> >
> > def my_io_process
> > session[:message] = "Starting..."
> > do something
> > session[:message] = "Updated message"
> > do something else.. etc
> > session[:message] = nil
> > end
> >
> >
> > Create another controller method that just returns the value of
> > session[:message].
> >
> > def update_message
> > render :text => session[:message]; :layout => false
> > end
> >
> > So now, all you need is a periodic routine that updates a div on the page
> > with the session[:message] contents. You can either update the div
> directly
> > from this call, or like I do, use RJS to do other things on the page. This
> > polling is turned on & off by the javascript variable poll_message
> >
> > <%= periodically_call_remote(
> > :condition => "poll_message == true",
> > :frequency => 2,
> > :url => { :action => 'update_message', :only_path => false }
> > ) %>
> >
> >
> > You can turn poll_message on & off like this, just include this code in a
> > RJS update whenever you want to change turn the polling on & off.
> >
> > <script type="text/javascript">
> > //<![CDATA[
> > <% if @session[:message] && !@session[:message].blank? %>
> > poll_message = true;
> > <% else %>
> > Poll_message = false;
> > <% end %>
> > //]]>
> > </script>
> >
> >
> > Rgds,
> > - matt.
> >
> > -----Original Message-----
> > From: ruby-talk@hinv.org [mailto:ruby-talk@hinv.org]
> > Sent: 31 March 2007 17:56
> > To: ruby-talk ML
> > Subject: updating an RJS template _while_ executing a method
> >
> > I have a method doing I/O stuff which takes quite a long time.
> > While this method is processed,
> > I want the user of my tool to be informed of the current execution state.
> >
> > For example,
> > if the method opens a file,
> > I want my RJS template to render a message such as "opening file XYZ...",
> > if the method changes to a directory, message would be "cd to ...".
> >
> > As far as I can tell,
> > RJS templates are rendered AFTER the corresponding method is executed.
> > But I want them to run (sort of) synchronously.
> > Or at least have a way to trigger messages while a method is executed.
> >
> > Cheers,
> > Tom.
> >
> >
>
>