Ajax Callbacks in Rails 3 with prototype, not jQuery

I upgrade the application from Rails 2 to 3 and recycle all removed functions to use Unobtrusive Javascript. Where I'm afraid this is handling ajax callbacks in UJS.

There are many resources that I found that show how to implement these callbacks using jQuery, but not much for the prototype. Perhaps you can help me figure this out.

In Rails 2, I had the following:

<% remote_form_for @foo, {:loading => "loading_function()", :complete => "complete_function()" } do |f| %>
 ...
<% end %>

In Rails 3, I have the following:

<%= form_for @foo, :remote => true do |f| %>
 ....
<% end %>

From what I have guessed so far (which might be wrong), I need to attach my old upload / exit functions to the form so that they are fired by the handleRemote function in Rails.js. I just don't know how to do this.

Again, I am doing this in Prototype. Therefore, responses related to this structure are evaluated.

+3
4

:

<%= form_for @foo, :remote => true do |f| %>
 ...
<% end %>
...
<script type='text/javascript'>
  $('edit_foo').observe('ajax:before', loading_function());
  $('edit_foo').observe('ajax:complete complete_function());
</script>
+5

. , JQuery, JQuery Prototype , . , - Prototype:

views/tasks/_newform.html.erb:
<%= form_for(@task, :remote => true) do |f| %>
  <div>
    <%= f.label 'Add a new task: ' %>
    <%= f.text_field :name %>
  </div>
  <div>
    <%= f.submit %>
  </div>
<% end %>

views/tasks/index.html.erb:
<div id='newform'>
  <%= render :partial => "newform", :locals => { :@task => Task.new } %>
</div>

views/tasks/create.js.rjs:
page.insert_html :after, 'tablehead', :partial => @task
page.replace_html 'newform',:partial => "newform", :locals => { :@task => Task.new }

: "format.js"

0

Rails 2.3.x .

, ': update', :

remote_form_for(@obj, :update => "new_obj", :before => "some js code") do |f|

_.


, :update UJS Rails 3. rails.js Rails 3 :remote => true Ajax.Request(...), :update Rails 2 Ajax Ajax.Updater(...). , :update Rails 2, 2 :

  • jquery-rails, Ajax , :

    $("#elem").bind("ajax:complete", function(et, e){
      $("#results").html(e.responseText);
    });
    
  • Prototype, ajax, Ajax.Updater(...) Ajax.Request. :remote => true, Ajax.Request.

. , ajax:complete

$('new_obj').observe('ajax:complete', function(request){
  console.info(request);
});

The object requestdoes not contain an answer anywhere. This is pretty massive, so I could be wrong. Hope this helps someone else try upgrading from Rails 2 to 3.

0
source

There is a way to get an answer from an Ajax.Request call if you used the remote_form_for parameter with: update. So you probably don't need to change it to use Ajax.Updater as a workaround. Basically, you use respone.memo.responseText, in your example it will be something like this:

$('new_obj').observe('ajax:complete', function(response){
  console.info(response.memo.responseText);
  // Probably you would use it like this:
  $('new_obj').update(response.memo.responseText);
});
0
source

All Articles