Topic: Passing parameters to application.js on :loading/:complete

I'm making an AJAX call on a generically generated element and for the
:loading and :complete parameters I need to be able to pass the id of
the div that I want to be affected.  How do I do this?

Current effort:

In my view:

<%= form_remote_tag :url => { :action => 'reply' },
                    :loading => 'PostLoader.disableReplyForm(id)',
                    :complete => 'PostLoader.enableReplyForm(id)',
                    :html => { :id => id.to_s + '-reply-form' } %>
<%= image_tag 'ajax-loader-light.gif', :id => @id.to_s +
'-form-indicator', :style => 'display:none;' %>

and in my application.js:

        disableReplyForm: function(id) {
                Element.show(id + '-form-indicator');
                Form.disable(id + '-reply-form');
        },

        enableReplyForm: function(id) {
                Element.hide(id + '-form-indicator');
                Form.enable(id + '-reply-form');
        },

The names of the elements are generated fine (e.g. 14-reply-form), but
I can't seem to figure out the way that rails wants me to pass in the
value for the variable "id", maybe somebody can help.

Thanks a lot,

Andrew

Re: Passing parameters to application.js on :loading/:complete

Let me see if I understand. Do you want to pass the ruby id variable to the JavaScript function? Try this:

<%= form_remote_tag :url => { :action => 'reply' },
                    :loading => "PostLoader.disableReplyForm('#{id}')",
                    :complete => "PostLoader.enableReplyForm('#{id}')",
                    :html => { :id => id.to_s + '-reply-form' } %>

I'd recommend trying out RJS for this. That way you won't need to create custom JavaScript functions. See this post for more information on that.

Railscasts - Free Ruby on Rails Screencasts

Re: Passing parameters to application.js on :loading/:complete

Ryan,

That worked perfectly.  Thanks a lot.

I was considering using RJS (I use RJS in other parts of my app and it works wonderfully), but I wasn't sure exactly how to pull it together, especially with the syncs.

I bought the O'Reilly PDF (which is amazing) and maybe I haven't gotten to that bit yet, but maybe you'd be able to drop a quick word on execution.

Should I:

1) package the AJAX load indicators in the RJS action that is executed by the form (meaning I don't set the loading and complete options, but trigger those in the RJS indicated by the action)
or
2) is there a way that I can trigger RJS actions in the loading and complete options?  Because if that's the case, then it would buy me a ton of flexibility and I wouldn't need to cram all this into application.js (though I got this application.js example from the O'Reilly PDF)

Regardless, thank you very much for debugging this (admittedly non-AJAX) issue.  I tried the hash escape, but with single quotes, ack.

Andrew

Re: Passing parameters to application.js on :loading/:complete

Oops, I wasn't thinking. RJS can't replace what you are doing with :loading, only :complete. The way you are doing it is probably best.

Railscasts - Free Ruby on Rails Screencasts