Common RJS

I want to have some RJS executed whenever an xhr request is made. My
initial thought was an "application.rjs" might do this, however that
does not seem to be the case.

Is there a way to set up some RJS that is executed whenever RJS is
fired? I have code that I don't want to repeat all over the place?

Failing the above. I thought adding a helper function the application.rb
and calling that from each .rjs file. However this does not work either.
Is there a way to call a method defined in applicaiton.rb from any .rjs
file?

thanks!

in your controller try something like this.

format.js { render :template => "shared/error.js.rjs" }

John I

Not sure I understand you John - can you clarify what you mean by

in your controller try something like this.

format.js { render :template => "shared/error.js.rjs" }

Are you saying put something in my application controller (if so,
where?) or in the controller action method that is being fired before
the controller/action.rjs file is executed?

If it's the latter then that would mean repearing it on every action
that has RJS.

What I'm really looking for is if Rails has a way of invoking a common
RJS file on every RJS action.

My reasoning for this is there are "core" tasks that I'd like to do
using Ajax, such as showing/hiding my flash bar depending on whether
there is a message to display.

Many thanks for your help.

I want to have some RJS executed whenever an xhr request is made. My
initial thought was an "application.rjs" might do this, however that
does not seem to be the case.

Is there a way to set up some RJS that is executed whenever RJS is
fired? I have code that I don't want to repeat all over the place?

Failing the above. I thought adding a helper function the
application.rb
and calling that from each .rjs file. However this does not work
either.
Is there a way to call a method defined in applicaiton.rb from
any .rjs
file?

put this in your application.rb (or any appropriate helper file)

def some_common_stuff
   update_page do |page|
     page.some_rjs_stuff
     ...
   end
end

Then in your rjs files you can do

page << some_common_stuff

Fred

This was modified from peepcodes RJS screencast ( I updated it for
rails 2)
http://peepcode.com/products/rjs-templates

They use a task list for the demo.
In the tasks_controller

It definitely should be application_helper (my bad, not quite typing what i was thing). It would be awfully helpful to know what the error was (check the log files)

Fred

> page << rjs_update_flash
> My log shows a 500 internal error.
> I also tried putting the method in application_helper.rb but that
> didn't
> work either.

This does work (on Rails 2.0.2 at least):

in application_helper.rb:

module ApplicationHelper
  def display_flash(flash)
    page.replace_html('FlashPane', flash[:notice])
  end
end

in your "create.js.rjs" file:

page.display_flash(flash)

You need to pass the flash hash in specifically. I am using this now,
it works really nicely - not just for flash, you can put any RJS in
there you want.

Mikel
http://lindsaar.net/