views:

107

answers:

1

I just started using RJS which is awesome however I am used to putting visual effects in the link_to_remote and such and I'm not sure how to trigger actions before and after remotes are triggered.

Take this link for example:

HTML

<span id="<%= "edit_comment_link_#{comment.id.to_s}"%>" style="float:left;"> 
<%= link_to_remote "edit", {:update => "comment_#{comment.id.to_s}", :url => edit_post_comment_path(comment.post, comment), :method => :get}%> | </span>

Controller:

def edit
  @comment = Comment.find(params[:id]) 
  respond_to do |format|
    #format.html render edit_comment_path(@comment)
    format.js 
  end
end

RJS:

page.replace_html "edit_comment_link_#{@comment.id.to_s}", "currently editing | "

So is RJS mainly for after actions are rendered visual effects such as a spinner should be put into the link_to_remote with call_backs? Is this a good way of doing things?

A: 

Is this what you mean?

<%= link_to_remote '<span>edit</span>', 
        :url => edit_post_comment_path(comment.post, comment),
        :method => :get,
        :loading => "Element.show('spinner')",
        :complete => "Element.hide('spinner')" -%>

In the rjs file you can stack effects/events with page.delay:

  item = "edit_comment_link_#{@comment.id.to_s}"
  page.replace_html  item,  :text => 'currently editing | '
  page.visual_effect :fade, item, :duration => 0.5
  page.delay 0.5 do
    page.visual_effect :highlight, "other_dom_object", :duration => 0.5
  end

more info on rjs events here: JavaScriptGenerator API Docs

inkdeep