RJS partials - or - DRY RJS
|
|
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’
any ideas? |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |