RJS partials - or - DRY RJS

Subscribe to RJS partials - or - DRY RJS 5 posts, 3 voices

 
Avatar Andy Koch 10 posts

I’ve got one for ya softies,

I’ve got a bit of RJS to refresh the flash div on the page, this bit is repeated in many RJS templates. Logically, I’d like to extract it into some kind of RJS partial.

for example,

I’d like to have this bit in one RJS….
if !flash.empty? page.visual_effect :pulsate, :flash_messages, :pulses => 2, :queue => ‘queue_flash’ flash.discard end
now how to insert this in this RJS template (and all the others)
page.replace_html :flash_messages, :partial => ‘layouts/flash’ page.replace_html :content, :partial => ‘strings’
  1. and replace this with some kind of include if !flash.empty? page.visual_effect :pulsate, :flash_messages, :pulses => 2, :queue => ‘queue_flash’ flash.discard end page.visual_effect :appear, ‘content’, {:duration => 0.3, :queue => ‘queue’} **

any ideas?

 
Avatar Andy Koch 10 posts

ok, just discovered RJS helpers

to conclude – add a method in regular view helper module using page,

like def display_flash_messages

end

then in RJS call

page.display_flash_messages

 
Avatar gammons 2 posts

Instead of writing .rjs files I typically render updates to a page in the controller.

render :update do |p| p.replace_html :contacts, :partial => “contacts_partial” end return

 
Avatar Andy Koch 10 posts

yes, rendering in controllers is where I started, but then things started to get more involved and all the rjs code started to really clutter up my controllers – hence the rjs files

 
Avatar Ismail Degani 1 post

Hi Andy, (and other softies)

I got this helper approach to work, but the problem is that my RJS needs a lot of the controller’s instance variables, and the helper doesn’t have access to this context.

I’m using YM4R to work with google maps, and I have an rjs that basically clears the maps and displays a few default markers retrieved from a database. I have other rjs files that do more complicated rendering, but before that they all need the same “reset” rjs code. What do you recommend?

Thanks, Ismail