Forum: Ruby on Rails RJS templates before, during and after

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-02-08 06:11
(Received via mailing list)
RJS templates are nifty.  They let you put all sorts of fun effects all
over your bland html pages without too much work.

The link_to_remote function also lets you run scripts before, during,
and after an AJAX call is run by using callbacks.

There does not seem to be an equivalent method for running an RJS
template before or during an action.  Rendering it at the end is no
problem.

One could write a helper function to do all the lifting and use the
:before, and :loaded callbacks in the standard way and then let the RJS
template handle the rest, but this seems like a poor solution.

Anyone have any bright ideas about how to render one RJS template at the
beginning of a call and one at the end? (and no, I don't think
before_filters will work).

_Kevin
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-02-08 19:04
(Received via mailing list)
Actually, the same options you use in the 'old school' remote calls
still work with RJS calls. The only option that doesn't work, and
seems to break it is the :update option. I'm not sure if you were
asking if there was a way to render an RJS template from one of these
filters, and that I'm not sure of.

Everything else though should work just like before.

-Nick

On 8 Feb 2006 05:09:03 -0000, Kevin Olbrich
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-02-08 19:41
Nick Stuart wrote:
> Actually, the same options you use in the 'old school' remote calls
> still work with RJS calls. The only option that doesn't work, and
> seems to break it is the :update option. I'm not sure if you were
> asking if there was a way to render an RJS template from one of these
> filters, and that I'm not sure of.
>
> Everything else though should work just like before.
>
> -Nick
>
> On 8 Feb 2006 05:09:03 -0000, Kevin Olbrich

Yeah, basically I'm looking for a way to have the action control the
'before' callback.  The idea of having to specify a :before callback
everytime I add an AJAX action and then letting the RJS template handle
the :complete seems.... strange and anti-DRY.

What I would like to see is some mechanism for allowing AJAX actions to
initiate a 'before' RJS template when the action starts, and then render
the 'complete' RJS action like it does now.

I have a feeling that this is harder than it sounds.

If you specify a 'before_filter', then the Ajax listener on the client
side just handles the first thing that comes back, and then stops
listening.  So the final RJS template never gets rendered.

Probably the best solution to this is to allow the link_to_remote
function to accept an RJS template file for the :before, and :loading
callbacks.  At least this way the callback functions can be complicated
and easily changed without having to be hard-coded.

_Kevin
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-02-08 19:51
(Received via mailing list)
I think the main problem with this idea is that the before, after,
whatever, callbacks all take place on the client side completely in
java script. I don't think they were intended for anything more then
simple notification type deals.

You are still going to have to have the before/after filters in the
link no matter what you do I believe. If you have them in the RJS
template then it kind of defeats the purpose as you've already made
the remote call, or returned from it.

The only you might be able to move this stuff to RJS templates is to
have the before and after calls actually call an action on some
controller that renders it. You would have to do this synchronously
though before the main call is fired (I think, it may not matter), but
I'm not sure what would be involved in this.

Sorry I couldn't be of more help. =\

-Nick
This topic is locked and can not be replied to.